改变tabbar的选中
的item的字体
颜色
self.tabbar.tintColor = [UIColor redColor];
改变tabbar未选中
的item的字体
颜色
self.tabbar.unselectedItemTintColor = [UIColor yellowColor];
改变tabbar的背景颜色
self.tabbar.barTintColor = [UIColor redColor];
self.tabbar.translucent = NO;
[[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]];
self.tabbar.translucent = NO
干了什么
YES
的时候,tabbar
的视图层级是UITabBar
-->UIBarBackground
-->UIVisualEffectView
-->UIVisualEffectBackdropView
-->UIVisualEffectSubview
-->UIVisualEffectSubview (决定tabbar颜色的视图)
NO
的时候,tabbar
的视图层级是UITabBar
-->UIBarBackground
-->UIImageView (决定tabbar颜色的视图)
YES
还是NO
,从颜色效果上,我没看出区别使用方法2
的时候,视图层级
和self.tabbar.translucent = NO
一摸一样,都是3层
改变tabbar
的背景颜色,又看到下面这个方法
UIView *color_view = [[UIView alloc]initWithFrame:self.tabBar.bounds];
color_view.backgroundColor = [UIColor redColor];
[self.tabBar insertSubview:color_view atIndex:0];
这个方法只是改变tabbar部分的背景颜色,tabbar到底部的安全区safeArea
有一条是改变不了的。比如使用上面的方法设置tabbar
为redcolor
,tabbar下面的安全区有一条留白。
// 这是当self.tabBar.translucent = YES时,tabBar的UIVisualEffectView背景色
if (@available (iOS 15.0, *)) {
// iOS 15.0 及以上
UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
[appearance configureWithOpaqueBackground];
appearance.backgroundColor = RGBA(236, 231, 222, 1);
self.tabBar.standardAppearance = appearance;
self.tabBar.scrollEdgeAppearance = self.tabBar.standardAppearance;
} else {
self.tabBar.barTintColor = RGBA(236, 231, 222, 1);
}
/**
// 当tabBar.translucent = NO 时,可以直接设置 tabBar.backgroundColor:
self.tabBar.translucent = NO;
self.tabBar.barTintColor = RGBA(236, 231, 222, 1);
self.tabBar.backgroundColor = UIColor.redColor;
*/
/**
// swift:
if #available(iOS 15.0, *) {
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = customColor
self.tabController.tabBar.standardAppearance = appearance
self.tabController.tabBar.scrollEdgeAppearance = self.tabController.tabBar.standardAppearance
}
*/