微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AppDelegate和TabBarViewController

如何解决AppDelegate和TabBarViewController

我写了如下代码

AppDelegate.swift

func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 9,weight: .bold),.foregroundColor: UIColor.init(red: 50/255,green: 50/255,blue: 50/255,alpha: 1)],for: .normal)

    return true
}

然后,我转到一个选项卡上的另一个Viewcontoller屏幕。同时,我按照描述的那样编写代码

nav.modalPresentationStyle = .fullScreen

然后,关闭ViewContoroller屏幕。然后,其他选项卡文本颜色已更改为认颜色(蓝色)。为什么会这样?

并且可以通过编写如下所述的代码解决该问题:

TabBarViewController.swift

override func viewWillAppear(_ animated: Bool) {
    let color = UIColor.init(red: 50/255,alpha: 1)
    UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 9,.foregroundColor: color],for: .normal)
    tabBar.tintColor = color
}

分享一个有关该问题的视频,以清楚地向您解释。 请检查它。

https://streamable.com/z9d1j5

解决方法

您没有像这样配置所选颜色:

UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 9,weight: .bold),.foregroundColor: yourcolor],for: .selected)

编辑:

如果您在iOS 13+上运行,则应按以下方式配置UITabBarAppearence:

 let tabBarItemAppearence = UITabBarItemAppearance()
 tabBarItemAppearence.normal.iconColor = UIColor.yourcolor
 tabBarItemAppearence.selected.iconColor = UIColor.yourcolor
            
 tabBarItemAppearence.normal.titleTextAttributes = [.foregroundColor: UIColor.yourcolor]
 tabBarItemAppearence.selected.titleTextAttributes = [.foregroundColor: UIColor.yourcolor]

 let tabBarAppearence = UITabBarAppearance()
 tabBarAppearence.inlineLayoutAppearance = tabBarItemAppearence
 tabBarAppearence.compactInlineLayoutAppearance = tabBarItemAppearence
 tabBarAppearence.stackedLayoutAppearance = tabBarItemAppearence
 tabBarAppearence.backgroundColor = color

然后像这样设置外观

tabBarController.tabBar.standardAppearance = tabBarAppearence

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。