由于项目要使用到选项卡,所以使用了UINavigationController 嵌套 UITabBarController搭建一个简单的模型,欢迎各位一起讨论其中的不足,一起学习,一起进步。
首先,写了一个ViewController继承 UITabBarController 易修改和复用,名字为 BaseTabBarController 主要有两个方法,用于设置UITabBarController里包含的每个viewcontroller和背景。
/**
* 设置选项卡ViewController
*
* @param viewController viewController
* @param selectedImage 选中的图片
* @param customImage 默认图片
* @param title 标题
* @param highLigthedColor 标题选中颜色
* @param customColor 标题默认颜色
* @param tag tag
* @return UINavigationController
*/
-(UINavigationController *)setTabViewController:(UIViewController *)viewController
selectedImage:(UIImage *) selectedImage
customImage:(UIImage *)customImage
title :(NSString *)title
highLigthedColor :(UIColor *)highLigthedColor
customColor :(UIColor *)customColor
tag:(NSInteger)tag
{
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.navigationBar.hidden = YES;
UITabBarItem * icon = [[UITabBarItem alloc] init];
icon.tag = tag;
[icon setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:customImage];
if(title != nil){
[icon setTitle:title];
[icon setTitleTextAttributes:@{UITextAttributeTextColor :customColor} forState:UIControlStateNormal];
[icon setTitleTextAttributes:@{UITextAttributeTextColor :highLigthedColor} forState:UIControlStateHighlighted];
}
navigationController.tabBarItem = icon;
return navigationController;
}
/**
* 设置选项卡的背景颜色
*
* @param colorImageView 颜色图片
*/
-(void)setBackGroundColor:(UIImageView *)colorImageView
{
[[UITabBar appearance] setBackgroundImage:IMG(@"color_2")];
}
然后在你需要用到选项卡的ViewController 中 继承BaseTabBarController
然后添加对应的VIewcontroler
HoemPageViewController * homePageViewController = [[HoemPageViewController alloc] init];
UINavigationController * nav_homePageViewController = [self setTabViewController:homePageViewController
selectedImage:IMG(@"tabbar_home_selected_os7")
customImage:IMG(@"tabbar_home_os7")
title:@"首页"
highLigthedColor:UIColorFromRGB(0x067AB5)
customColor:UIColorFromRGB(0x73c34a)
tag:1];
DiscoverViewController * disCoverViewController = [[DiscoverViewController alloc] init];
UINavigationController * nav_disCoverViewController = [self setTabViewController:disCoverViewController
selectedImage:IMG(@"tabbar_discover_selected_os7")
customImage:IMG(@"tabbar_discover_os7")
title:nil
highLigthedColor:nil
customColor:nil
tag:2];
就暂写两个为例,需要多的再添加,一般都是4或5个。
接下创建一个数组,存上面创建viewcontroller
NSArray * views = @[nav_homePageViewController,nav_disCoverViewController];
self.viewControllers = views;
self.delegate = self;
然后实现代理:
#pragma UITabBarControllerDelegate delegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//选中的时候
switch (viewController.tabBarItem.tag) {
//根据tag来判断选中的是哪个选项
default:
break;
}
//如果您觉得系统自带的小红圈数字不喜欢的话,下面提供一种方式,可以用图片代替系统的小红圈。
// 自定义红色小数字
for(UIView *view in tabBarController.tabBar.subviews) {
if([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}
UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"color_2"]];
image.frame = CGRectMake(30, 0, 20, 20);
[tabBarController.tabBar addSubview:image];
}
下面是关于 UINavigationController 和 UITabBarController 的一些属性设置小知识
设置标题栏的的颜色,ios7的话使用 [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
iOS 6 使用 _navigationController.navigationBar.tintColor = UIColorFromRGB(0x067AB5);
ios6及一下选中某一项的时候都会有一个灰色的背景框,可以使用下面该方法,用一张透明的图片把他给去掉,如果您有更好的方法也希望提供一起学习。
[[UITabBar appearance] setSelectionIndicatorImage:IMG(@"item_selected_bg")];
好了 ,收工,准备下班。。。
本文介绍了如何在iOS项目中使用UINavigationController嵌套UITabBarController搭建选项卡模型。通过自定义BaseTabBarController,设置选项卡的ViewController、背景颜色,并展示实例代码。还分享了关于UINavigationController和UITabBarController的一些属性设置技巧。

1013

被折叠的 条评论
为什么被折叠?



