首先我们需要搭建一个空的项目,当然xcode6.0以后不支持直接创建空项目,所以我们需要在系统生成项目之后,删除xcode自动给你生成的控制器和storyboard,另外需要在Main Interface 处将之前的main删除。如图所示:
然后我们需要在Appdelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法编辑如下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; // 在这里创建一个tabbarcontroller UITabBarController *tabBarController = [[UITabBarController alloc]init]; // 把创建好的tabbar设置成rootviewcontroller self.window.rootViewController = tabBarController; // 在tabbar里边添加子控制器 UITableViewController *vc1 = [[UITableViewController alloc]init]; // 设置导航栏的标题 vc1.title = @"消息列表"; UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu_icon_bulb"] style:UIBarButtonItemStylePlain target:self action:nil]; vc1.navigationItem.rightBarButtonItem = barBtn; // 将一般控制器 转换为导航控制器 UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1]; // 设置tabbar的item项 的文字 nav1.tabBarItem.title = @"消息"; // 设置item 的图片 nav1.tabBarItem.image = [UIImage imageNamed:@"tab_recent_nor"]; // 设置显示数字 nav1.tabBarItem.badgeValue = @"20"; UITableViewController *vc2 = [[UITableViewController alloc]init]; //vc2.view.backgroundColor = [UIColor blueColor]; //NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil]; // 首先创建一个数组,内部存上UISegmentedControl按钮的文字 NSArray *segmentTitle = [[NSArray alloc]initWithObjects:@"分组",@"全部", nil]; UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:segmentTitle]; segment.selectedSegmentIndex = 0; //设置默认选择项索引 //设置样式 // segment.segmentedControlStyle = UISegmentedControlStylePlain; //设置在点击后是否恢复原样 // segment.momentary = YES; // [segment setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片 // 具体segmentcontrol的用法请参照: http://www.cnblogs.com/top5/archive/2012/05/17/2506618.html // 将按钮添加到navigationitem的导航栏上去 vc2.navigationItem.titleView = segment; UIBarButtonItem *barBtn2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"header_icon_add"] style:UIBarButtonItemStyleDone target:self action:nil]; vc2.navigationItem.rightBarButtonItem = barBtn2; UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2]; nav2.tabBarItem.title = @"联系人"; nav2.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"]; tabBarController.viewControllers = @[nav1,nav2]; // 显示在window窗口 [self.window makeKeyAndVisible]; return YES; }
运行之后的结果是: