iOS13 tabBar 设置背景色失效
一、首先看一下 tabBar 的UI 显示的层级结构
- 如果 UITabBarController 有 N 个子控制器,那么 UITabBar 的内部就会有 N 个 UITabBarButton 作为子控件。
- UITabBarButton 里面显示什么内容,由对应子控制器的 tabBarItem 属性决定。
二、升级到 iOS 13后,之前正常显示的 tabBar 的背景色设置失效了
解决方法如下,代码展示:
- oc 版本
(void)initialize {
NSDictionary *attrNormal = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kLightGrayColor};
NSDictionary *attrSelect = [NSDictionary dictionary];
UITabBar *tabBar = [UITabBar appearance];
//ios 13 之后需要这样设置才有效
if (@available(iOS 13.0, *)) {
attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor labelColor]};
UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc]init];
//设置tabar背景色
tabBarAppearance.backgroundColor = [UIColor secondarySystemGroupedBackgroundColor];
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNormal;
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect;
//必须要加上这两句
tabBar.standardAppearance = tabBarAppearance;
if (@available(iOS 15.0, *)) {
tabBar.scrollEdgeAppearance = tabBarAppearance;
} else {
// Fallback on earlier versions
}
} else {
attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kBlackColor};
UITabBarItem *tbItem = [UITabBarItem appearance];
[tbItem setTitleTextAttributes:attrNormal forState:UIControlStateNormal];
[tbItem setTitleTextAttributes:attrSelect forState:UIControlStateSelected];
[tabBar setBarTintColor:kWhiteColor]; //tabBar的背景色
}
tabBar.translucent = YES; //translucent: 半透明的
}
- Swift 版本
//设置全局界面的颜色
func setupAppearance() {
//设置加粗字体: Helvetica-Bold
let attrNomal = [NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 18), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
let attrSelect = [NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 18), NSAttributedString.Key.foregroundColor: UIColor.black]
let customTabBar = UITabBar.appearance()
if #available(iOS 13.0, *) {
let tabBarAppearance = UITabBarAppearance()
//设置tabar背景色
tabBarAppearance.backgroundColor = .secondarySystemGroupedBackground
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNomal as [NSAttributedString.Key : Any]
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect as [NSAttributedString.Key: Any]
customTabBar.standardAppearance = tabBarAppearance
if #available(iOS 15.0, *) {
customTabBar.scrollEdgeAppearance = tabBarAppearance
} else {
}
} else {
let tabBarItem = UITabBarItem.appearance()
tabBarItem.setTitleTextAttributes(attrNomal as [NSAttributedString.Key: Any], for: .normal)
tabBarItem.setTitleTextAttributes(attrSelect as [NSAttributedString.Key : Any], for: .selected)
//tabBar的背景色
customTabBar.barTintColor = .white
}
//translucent: 半透明的
customTabBar.isTranslucent = true
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgabaih
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01