【问题标题】:TabBar and NavigationBar do not change colorsTabBar 和 NavigationBar 不改变颜色
【发布时间】:2022-11-03 20:32:18
【问题描述】:

我在运行 xcode 14 和 ios 16 时遇到了一些问题。

导航栏不变色,Tab Bar 与背景颜色相同(我认为应该有不同的色调)。

class MainTabController: UITabBarController {
    
    // MARK: - Properties
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureViewControllers()
    }
    
    // MARK: - Helpers
    func configureViewControllers() {
        let feed = FeedController()
        let nav1 = templateNavigationController(image: UIImage(named: "home_unselected"), rootViewController: feed)
        
        let explore = ExploreController()
        let nav2 = templateNavigationController(image: UIImage(named: "search_unselected"), rootViewController: explore)
        
        let notifications = NotificationsController()
        let nav3 = templateNavigationController(image: UIImage(named: "search_unselected"), rootViewController: notifications)
        
        let conversations = ConversationsController()
        let nav4 = templateNavigationController(image: UIImage(named: "search_unselected"), rootViewController: conversations)
        
        viewControllers = [nav1, nav2, nav3, nav4]
    }
    
    func templateNavigationController(image: UIImage?, rootViewController: UIViewController) -> UINavigationController {
        
        let nav = UINavigationController(rootViewController: rootViewController)
        nav.tabBarItem.image = image
        nav.navigationBar.barTintColor = .white
        
        return nav
    }
}

我试过设置导航栏的外观:

        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = .white
        nav.navigationBar.standardAppearance = appearance
        nav.navigationBar.scrollEdgeAppearance = nav.navigationBar.standardAppearance

这似乎有点多余,您是否必须每次为每个导航控制器都这样做?

【问题讨论】:

    标签: ios swift uinavigationcontroller uikit uitabbarcontroller


    【解决方案1】:

    不,您不必为每个导航控制器执行此操作。

    您可以扩展UINavigationBar

    extension UINavigationBar {
        
        static func create() {
            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithOpaqueBackground()
            navBarAppearance.backgroundColor = .white
            appearance().standardAppearance = navBarAppearance
            appearance().scrollEdgeAppearance = navBarAppearance
            appearance().compactAppearance = navBarAppearance
        }
    }
    

    并从您的application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 拨打UINavigationBar.create()

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 2017-06-14
      • 2020-12-26
      • 2018-07-07
      • 2012-06-03
      • 2015-10-15
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多