tabbar 做个笔记。
-
改变tabbar的
选中的item的字体颜色self.tabbar.tintColor = [UIColor redColor]; -
改变tabbar
未选中的item的字体颜色self.tabbar.unselectedItemTintColor = [UIColor yellowColor]; -
改变tabbar的背景颜色
- 方法1
self.tabbar.barTintColor = [UIColor redColor]; self.tabbar.translucent = NO;- 方法2
[[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,从颜色效果上,我没看出区别 - translucent = NO 对UICollectionView截屏的影响,具体见第十一条
- 是
使用
方法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
}
*/