How to change background color of UINavigationItem?

后端 未结 4 1556
-上瘾入骨i
-上瘾入骨i 2021-01-04 07:03

I have a UINavigationItem, but I can\'t found anything beside tittle, prompt, and back button in attribute inspector

4条回答
  •  半阙折子戏
    2021-01-04 07:44

    One possible solution is to embed the View Controller it is containing the Navigation Item in a Navigation Controller and access to properties color of Navigation Bar:

    // Color title 'navigationItem'
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    
    // Background 'navigationBar'
    UINavigationBar.appearance().barTintColor = UIColor.blackColor()
    
    // Color title 'navigationBar'
    let color = UIColor.orangeColor()
    self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes(
        [NSForegroundColorAttributeName: color], forState: .Normal)
    

提交回复
热议问题