繁体   English   中英

AppDelegate 和 TabBarViewController

[英]AppDelegate and 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, green: 50/255, blue: 50/255, alpha: 1)
    UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 9, weight: .bold), .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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM