UITabBar、UINavigationBar设置

Author Avatar
新风作浪 3月 28, 2018
  • 在其它设备中阅读本文章

UITabBar

1
2
3
4
5
6
7
8
NSDictionary *normalTitleAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#bcc9d8"],NSFontAttributeName:[UIFont systemFontOfSize:10]};
NSDictionary *selectedTitleAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#367ee8"],NSFontAttributeName:[UIFont systemFontOfSize:10]};
[[UITabBarItem appearance] setTitleTextAttributes:normalTitleAttributes forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:selectedTitleAttributes forState:UIControlStateSelected];
[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
[[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
[[UITabBar appearance] setTranslucent:NO];

UINavigationBar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 如果需要设置字体就在字典中加入 [UIFont fontWithName:@"Hiragino Sans GB" size:14]
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]]; // 导航栏背景颜色
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]]; // 导航栏返回按钮、自定义UIBarButtonItem颜色
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; // 去除自带分割线
[[UINavigationBar appearance] setTranslucent:NO];
```
**translucent属性**
iOS 7 开始采用了扁平化设计,UINavigationBar和UITabBar附带了半透明效果,translucent属性能决定UITabBar/UINavigationBar是否为半透明的效果,默认为YES,即默认情况下为半透明效果
**设置title属性**

self.navigationItem.title = @”my title”; //sets navigation bar title.
self.tabBarItem.title = @”my title”; // sets tab bar title.
self.title = @”my title”; // sets both of these.

1
2
**UITabBar、UINavigationBar icon图标渲染问题**

UIImage *leftBarButtonImage = [[UIImage imageNamed:@”navbar_back_normal”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftBarButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];

vc.tabBarItem.title = title;
vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

1
2
**UIBarButtonItem 间距偏移问题**
UIImage *leftBarButtonImage = [[UIImage imageNamed:@"navbar_back_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

if (@available(iOS 11.0, *)) {
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 44, 44);
    [leftButton setImage:leftBarButtonImage forState:UIControlStateNormal];
    [leftButton setImage:leftBarButtonImage forState:UIControlStateHighlighted];
    leftButton.contentEdgeInsets =UIEdgeInsetsMake(0, -20,0, 0);
    leftButton.imageEdgeInsets =UIEdgeInsetsMake(0, -15,0, 0);
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItems=@[leftBarButtonItem];

}else{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftBarButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:self action:@selector(back)];
    spacer.width = -8;
    self.navigationItem.leftBarButtonItems = @[spacer, self.navigationItem.leftBarButtonItem];
}
1
2
3
4
**UIBarButtonItem frame**
获取self.navigationItem.leftBarButtonItem的frame,因为UIBarButtonItem继承UIView,所以要获取View对象然后在操作

UIView *view = [self.navigationItem.leftBarButtonItem valueForKey:@”view”];
```