我项目的主页常见的是UITabbarController + UINavigationController形式
因为导航栏是自定义的,所以需要把所有导航栏都隐藏掉,再在每个界面自己写导航栏视图(感觉好坑啊,以后应该会改)
因此,我写了重写了
一个继承于UINavigationController的NavigationController
和一个继承于UITabBarController的TabBarController。
一. TabBarController中的代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the
//A,(B、C一样)
ViewControllerA *aVC = [ViewControllerA new];
aVC.tabBarItem.title = @"主页";
aVC.tabBarItem.image = [[UIImage imageNamed:@"a_unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
aVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"a_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NavigationController *aNavi = [[NavigationController alloc] initWithRootViewController:aVC];
//设置tabBar背景色
[self.tabBar setBackgroundImage:[Tool createImageWithColor:UIColorFromHex(0x63DDA1)]];
//字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorFromHex(0xEE9754)} forState:UIControlStateSelected];
self.viewControllers = @[aNavi, bNavi, cNavi];
}
二. 在NavigationController中
之前的写法(×):
//重写方法[pushViewController: animated:]
//可拦截NavigationController子控制器中所有的push操作,
//因此在其中设置一句隐藏代码,其他界面就都不用设置了。
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//隐藏tabbar,(A、B、C界面上不隐藏,其他界面都隐藏)
//一定要在push之前隐藏,否则无效,
//所以,如果在调用 [super pushViewController: animated:] 后再写判断隐藏是无效的
if ([viewController isKindOfClass:[ViewControllerA class]] ||
[viewController isKindOfClass:[ViewControllerB class]] ||
[viewController isKindOfClass:[ViewControllerC class]])
{
viewController.hidesBottomBarWhenPushed = NO;
}
else {
viewController.hidesBottomBarWhenPushed = YES;
}
//先调用父类push
[super pushViewController:viewController animated:animated];
//隐藏导航栏,
[viewController.navigationController setNavigationBarHidden:YES];
}
更正(√):
//旧的写法不仅繁琐,而且,当要push的页面在tabBar中也有时处理会很麻烦。
//只要navigationController.viewControllers的第一个控制器隐藏就行了
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ((self.viewControllers.count > 0) && (viewController != self.viewControllers[0])) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
[viewController.navigationController setNavigationBarHidden:YES];
}
然后就ok了。
另外,关于TabBar设置背景颜色,我遇到两个坑
使用代码直接设置背景颜色居然无效!
//无效
[[UITabBar appearance] setBackgroundColor:UIColorFromHex(0x63DDA1)];
百度了下得设置背景图片才有效,经试验 下面3中设置方法均有效
//1.
[self.tabBar setBackgroundImage:[Tool createImageWithColor:UIColorFromHex(0x63DDA1)]];
//2.
[[UITabBar appearance] setBackgroundImage:[Tool createImageWithColor:UIColorFromHex(0x63DDA1)]];
//3. 第3种千万别忘了设置self.tabBar.opaque = YES,否则还是会出现tabBar的效果
UIView *tabBarBgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tabBar.bounds.size.width, self.tabBar.bounds.size.height)];
tabBarBgColorView.backgroundColor = UIColorFromHex(0x63DDA1);
[self.tabBar insertSubview:tabBarBgColorView atIndex:0];
self.tabBar.opaque = YES;
把16进制转换成颜色的宏定义:
#define UIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s & 0xFF00) >> 8))/255.0 blue:((s & 0xFF))/255.0 alpha:1.0]
#define UIColorFromHexA(s, a) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s & 0xFF00) >> 8))/255.0 blue:((s & 0xFF))/255.0 alpha:a]
#define UIColorFromRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
用颜色生成图片的代码:
//用颜色创建一张图片
+ (UIImage *)createImageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}