how to change navigationitem title color

前端 未结 11 1319
南旧
南旧 2021-01-30 12:35

I think all day to change the navigation Bar title color, but it doesn\'t work. this is my code:

var user: User? {
    didSet {
        navigationItem.title = us         


        
相关标签:
11条回答
  • 2021-01-30 13:04

    Swift 5/Xcode 11

    Write this code as extension from UINavigationBar

    extension UINavigationBar {
        func customNavigationBar() {
            // color for button images, indicators and etc. 
            self.tintColor = UIColor.Custom.mainAppColor
    
            // color for background of navigation bar
            // but if you use larget titles, then in viewDidLoad must write
            // navigationController?.view.backgroundColor = // your color
            self.barTintColor = .white
            self.isTranslucent = false
    
            // for larget titles 
            self.prefersLargeTitles = true
    
            // color for large title label
            self.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
    
            // color for standard title label 
            self.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
    
            // remove bottom line/shadow
            self.setBackgroundImage(UIImage(), for: .default)
            self.shadowImage = UIImage()
        }
    }
    

    then in AppDelegate.swift call UINavigationBar.appearance().customNavigationBar() in didFinishLaunchingWithOptions function

    0 讨论(0)
  • 2021-01-30 13:06

    For iOS 13 you have to change the color in the appearance property and for older iOS versions you can do it directly in the navigation bar property.

    if #available(iOS 13.0, *) {
        navigationController?.navigationBar.standardAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    } else {
        navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
    }
    
    0 讨论(0)
  • 2021-01-30 13:09

    The didSet is called if your set the user, maybe you're setting the user's name variable and expecting the program to enter didSet. try setting the user.

    And if you want to change the color of the text when the navigation title is changed to the name of the user just call this code.

    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red]

    var user: User? {
        didSet {
            navigationItem.title = user?.name
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let newUser = User()
        newUser.name = "John Doe"
        user = newUser
    }
    
    0 讨论(0)
  • 2021-01-30 13:12

    in objetive c:

    [UINavigationBar appearance] setTitleTextAttributes: 
        [NSDictionary dictionaryWithObjectsAndKeys: 
            [UIColor blackColor], NSForegroundColorAttributeName, 
               [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]];
    
    0 讨论(0)
  • 2021-01-30 13:13

    Solution for iOS 13

    To customize the appearance of a navigation bar you need to use UINavigationBarAppearance:

    let appearance = UINavigationBarAppearance()
    appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
    appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.red]
    
    navigationItem.standardAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    
    0 讨论(0)
  • 2021-01-30 13:13

    Swift 4

    create project and test this with ViewController easy to use

    import UIKit
    class ProfileViewController: UIViewController {
          override func viewDidLoad() {
            super.viewDidLoad()
            configureNavigationBar()
              
              }
            func configureNavigationBar() {
                navigationItem.title = "Profile"
       let textChangeColor =[NSAttributedString.Key.foregroundColor:UIColor.black]
    navigationController?.navigationBar.titleTextAttributes = textAttributes
                  navigationItem.rightBarButtonItem?.tintColor = .white
                navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "arrow_right").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
                  navigationController?.navigationBar.prefersLargeTitles = true
                navigationItem.rightBarButtonItem?.tintColor = .white
                        }
    }
    
    0 讨论(0)
提交回复
热议问题