class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setTabbar()
}
}
extension MyTabBarController {
// MARK: - 设置tabbar
func setTabbar() {
// MARK: TabBar背景颜色
view.backgroundColor = .white
UITabBar.appearance().backgroundColor = .white
UITabBar.appearance().isTranslucent = false //避免受默认的半透明色影响,关闭
/// 未选中颜色/字号
let normal = [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)]
/// 选中颜色/字号
let selected = [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)]
// MARK: - 去除黑线/修改颜色 (shadowImage/backgroundImage缺一不可)
if #available(iOS 13.0, *) {
let appearance = tabBar.standardAppearance
appearance.backgroundImage = UIImage()
appearance.backgroundColor = .clear
appearance.shadowColor = .clear
appearance.shadowImage = UIImage()
// appearance.shadowImage = image(UIColor.red) // 修改线条颜色
tabBar.standardAppearance = appearance
if #available(iOS 15.0, *) {
tabBar.scrollEdgeAppearance = appearance
}
} else {
tabBar.shadowImage = UIImage()
// tabBar.shadowImage = image(UIColor.red) // 修改线条颜色
tabBar.backgroundImage = UIImage()
}
// 字体偏移
let titlePositionAdjustment = UIOffset(horizontal: 0.0,vertical: 0.0)
// MARK: - 设置 tabBar 字体颜色/字号 (未选中/选中)
if #available(iOS 13.0, *) { // iOS13 无法成功设置tabBar未选中状态下的文字颜色
let inlineLayoutAppearance = UITabBarItemAppearance()
inlineLayoutAppearance.normal.titlePositionAdjustment = titlePositionAdjustment // iOS13 设置字体偏移
inlineLayoutAppearance.normal.titleTextAttributes = normal // iOS13 修改未选中的颜色
inlineLayoutAppearance.selected.titleTextAttributes = selected // iOS13 修改选中的颜色
let appearance = tabBar.standardAppearance
appearance.stackedLayoutAppearance = inlineLayoutAppearance
tabBar.standardAppearance = appearance
if #available(iOS 15.0, *) {
tabBar.scrollEdgeAppearance = appearance
}
} else {
UITabBarItem.appearance().setTitleTextAttributes(normal, for: .normal) // 修改未选中的颜色
UITabBarItem.appearance().setTitleTextAttributes(selected, for: .selected) // 修改选中的颜色
UITabBarItem.appearance().titlePositionAdjustment = titlePositionAdjustment // 设置字体偏移
}
// MARK: - tabBar 底部工具栏背景颜色 (以下两个都行)
// tabBar.barTintColor = UIColor.orange
// 设置图片选中时颜色必须设置(系统默认选中蓝色)
// UITabBar.appearance().tintColor = UIColor.lightGray
}
// MARK: - 根据颜色值画条线
func image(_ color: UIColor) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0.5)
UIGraphicsBeginImageContext(rect.size)
guard let context = UIGraphicsGetCurrentContext() else {
UIGraphicsEndImageContext()
return UIImage()
}
context.setFillColor(color.cgColor)
context.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
}
Swift-UITabBarController适配(iOS13/15)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.适配UITabBar 2.适配UINavigationBar 其中用到的宏和工具方法
- 【Swift】 iOS15导航栏适配 原理就不多说了,直接参考之前写的Object-C版本适配[https://w...
- iOS15导航栏适配 设置导航栏纯色/透明、解决ScrollView类上滑导航栏出现磨砂阴影的问题 Swift版导...
- JKSwiftExtension[https://github.com/JoanKing/JKSwiftExten...
- 2021年苹果秋季发布会之后,iOS15的推出,给开发带来了不少的适配问题。(可恶!) 没办法,那是爸爸。我改咯~...