iOS NavigationController 和 TabBar 详细解析以及用法说明

Posted by Calvin on 2017-04-12

在我们日常开发过程中,需要经常使用 navigationController 和 TabBar,这篇文章就这两个控件进行一下详细解析以及在开发过程中怎么使用。

设置导航栏背景颜色

[self.navigationController.navigationBar setBarTintColor:RGB(67, 188, 252)];

设置导航栏字体颜色

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

设置导航栏字体颜色和大小

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}];

设置导航栏背景图片

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBack@2x"] forBarMetrics:UIBarMetricsDefault];

设置导航控制器是否为半透明

self.navigationController.navigationBar.translucent = NO;

这个属性会影响视图的坐标系:默认情况是半透明,试图的起点坐标是从屏幕的左上角开始的,当设置为透明,起点坐标是在导航控制器下面的左上角开始的。

官方解释如下:

New behavior on iOS 7.
Default is YES.
You may force an opaque background by setting the property to NO.
If the navigation bar has a custom background image, the default is inferred
from the alpha values of the image—YES if it has any pixel with alpha < 1.0
If you send setTranslucent:YES to a bar with an opaque custom background image
it will apply a system opacity less than 1.0 to the image.
If you send setTranslucent:NO to a bar with a translucent custom background image
it will provide an opaque background for the image using the bar’s barTintColor if defined, or black
for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.

关于 navigationController 底部细线

细心的同学会发现在iOS默认的navigationController的底部有一根细线,仔细查看下图

20170412149197025824881.jpg

如图所示,我们可以修改navigationController的属性来达到保留或者去掉黑线的目的。

如果开发时设置了背景颜色,可以使用如下代码去除黑线

[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];

不管在开发时设置了背景颜色或者是背景图片,都可以使用下面的方法去除黑线

self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;

TabBar

TabbarItem选中时字体颜色

[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];

TabbarItem默认字体颜色

[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

消除Tabbar黑线

[[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];

设置Tabbar背景色

[[UITabBar appearance]setBackgroundColor:[UIColor whiteColor]];
[[UITabBar appearance]setBarTintColor:[UIColor whiteColor]];

设置Tabbar背景图片

UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 49)];
backView.image = [UIImage imageNamed:@"navBack@2x"];
[self.tabBar insertSubview:backView atIndex:0];
self.tabBar.opaque = YES;

设置Tabbar以及Item

#pragma mark --
#pragma mark -- 创建子控制器
- (void)createSubViewControllers{
OneViewController *One = [[OneViewController alloc]init];
CustomNavigationController *navOne = [[CustomNavigationController alloc]initWithRootViewController:One];
navOne.fullScreenPopGestureEnabled = YES;
TwoViewController *Two = [TwoViewController new];
CustomNavigationController *navTwo = [[CustomNavigationController alloc]initWithRootViewController:Two];
navTwo.fullScreenPopGestureEnabled = YES;
ThreeViewController *Three = [[ThreeViewController alloc]init];
CustomNavigationController *navThree = [[CustomNavigationController alloc]initWithRootViewController:Three];
navThree.fullScreenPopGestureEnabled = YES;
FourViewController *Four = [[FourViewController alloc]init];
CustomNavigationController *navFour = [[CustomNavigationController alloc]initWithRootViewController:Four];
navFour.fullScreenPopGestureEnabled = YES;
self.delegate = self;
self.viewControllers = @[navOne,navTwo,navThree,navFour];
}
#pragma mark --
#pragma mark -- 设置所有的、分栏元素项
- (void)setTabBarItems{
NSArray *titleArr = @[@"查单词",@"查词根",@"记单词",@"我的"];
NSArray *normalImgArr = @[@"licai",@"dis",@"my",@"dis"];
NSArray *selectedImgArr = @[@"licaiSele",@"disSele",@"dis",@"mySele"];
for (int i = 0; i<titleArr.count; i++) {
UIViewController *vc = self.viewControllers[i];
vc.tabBarItem = [[UITabBarItem alloc]initWithTitle:titleArr[i] image:[UIImage imageNamed:normalImgArr[i]] selectedImage:[[UIImage imageNamed:selectedImgArr[i]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]];
vc.tabBarItem.tag = i+1;
}
}