iOS13 - UITabBar未选中项目的tintColor不起作用

11
在Xcode 10中,unselectedItemTintColor属性正常工作,但在Xcode 11和iOS 13中,UITabbar的unselectedItemTintColor属性不起作用。
override func viewDidLoad() {
    super.viewDidLoad()

    myTabbar.unselectedItemTintColor = .red
}

iOS 13+的情况下,请参考以下代码:if #available(iOS 13.0, *) { let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero) statusBar.backgroundColor = UIColor.red // Set any desired color UIApplication.shared.keyWindow?.addSubview(statusBar) }这将在iOS 13及更高版本中创建一个自定义状态栏,并将其添加到应用程序窗口中。请注意,此代码仅适用于使用Swift编写的应用程序。 - Keshu R.
尝试这个:self.tabBar.unselectedItemTintColor = [UIColor lightGrayColor]; - Mayank
2个回答

19

iOS 13与Xcode 11

if #available(iOS 13, *) {
     let appearance = UITabBarAppearance()
     appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
     appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
     appearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
     appearance.stackedLayoutAppearance.selected.iconColor = UIColor.red
     myTabbar.standardAppearance = appearance
}
     

1
注意:如果您正在使用内联布局(例如在iPad上),则需要使用inlineLayoutAppearance而不是stackedLayoutAppearance - Tad
1
如果您在使用inlineLayoutAppearance时遇到标题被截断的情况,请查看此问题:https://dev59.com/h8Lra4cB1Zd3GeqPEjhA#69457332 - Tad

9

如果遇到:iOS 15 和 Xcode 13

if #available(iOS 15, *) {
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.backgroundColor = .white
    tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.red]
    tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.black]
    tabBarAppearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
    tabBarAppearance.stackedLayoutAppearance.selected.iconColor = UIColor.red
    tabBarView.standardAppearance = tabBarAppearance
    tabBarView.scrollEdgeAppearance = tabBarAppearance
}

enter image description here


你是我的英雄!这正是我整天在找的东西!谢谢你。 - undefined
很高兴听到它对你来说运作得很好!@Alexnnd - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接