UITabBar、UINavigationBar设置

UITabBar

        NSDictionary *normalTitleAttributes = @{NSForegroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont systemFontOfSize:10]};
NSDictionary *selectedTitleAttributes = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:10]};
[[UITabBarItem appearance] setTitleTextAttributes:normalTitleAttributes forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:selectedTitleAttributes forState:UIControlStateSelected];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000

if (@available(iOS 13.0, *)) {

UITabBarAppearance* appearance = self.tabBar.standardAppearance.copy;
appearance.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor]];
appearance.shadowImage = [UIImage new];
appearance.shadowColor = [UIColor clearColor];
UITabBarItemAppearance *inlineLayoutAppearance = [[UITabBarItemAppearance alloc] init];
[inlineLayoutAppearance.normal setTitleTextAttributes:normalTitleAttributes];
[inlineLayoutAppearance.selected setTitleTextAttributes:selectedTitleAttributes];
appearance.stackedLayoutAppearance = inlineLayoutAppearance;
self.tabBar.standardAppearance = appearance;
}

#else

[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
[[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
[[UITabBar appearance] setTranslucent:NO];

#endif

**UINavigationBar **

// 如果需要设置字体就在字典中加入 [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.

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];

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];
}

UIBarButtonItem frame

获取self.navigationItem.leftBarButtonItem的frame,因为UIBarButtonItem继承UIView,所以要获取View对象然后在操作

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

2019.11.13更新