uinavigationbar添加view - iOS 8中的NavigationBar栏,色调和标题文字颜色




uinavigationbar left button (12)

通过Swift 3.2(不是Swift 4.0)快速上手

    self.navigationController?.navigationItem.largeTitleDisplayMode = .always
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

    // unconfirmed but I assume this works:
    self.navigationController?.navigationBar.barTintColor = UIColor.white
    self.navigationController?.navigationBar.barStyle = UIBarStyle.black

状态栏中的背景文本仍为黑色。 如何将颜色更改为白色?

// io8, swift, Xcode 6.0.1 
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]

}


//在Swift 4中

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

为了在 Objective-C中 工作,我必须在CustomViewController的 viewWillAppear 中放置以下几行。

[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];

对于 Swift2.x, 此方法有效:

self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

对于 Swift3.x, 此方法有效:

self.navigationController?.navigationBar.barTintColor = UIColor.red

AppDelegate.swift 中的 application(_:didFinishLaunchingWithOptions:) 我放置了以下内容:

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]

(对于Swift 4或更早版本,请使用 NSAttributedStringKey 而不是 NSAttributedString.Key

对于 titleTextAttributesdocs 说:

您可以在文本属性字典中为标题指定字体,文本颜色,文本阴影颜色和文本阴影偏移量


在Swift 4.2中

var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]

在Swift 4.2版中将导航栏标题的文本颜色设置为白色:

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

如果要设置整个应用程序的色度和条形颜色,可以将以下代码添加到AppDelegate.swift中

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

    var navigationBarAppearace = UINavigationBar.appearance()

    navigationBarAppearace.tintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
    navigationBarAppearace.barTintColor = UIColor(red:0.76, green:0.40, blue:0.40, alpha:1.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    return true
`

导航栏设置了TintColor和tintColor


我喜欢亚历克斯的回答。 如果您想快速尝试在 ViewController 进行操作,请确保使用

viewWillAppear()
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    var nav = self.navigationController?.navigationBar
    nav?.barStyle = UIBarStyle.Black
    nav?.tintColor = UIColor.white
    nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
    //nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2
}


斯威夫特4.1

将功能添加到viewDidLoad

override func viewDidLoad() {
  super.viewDidLoad()

  setup()
}   

setup() 函数中添加:

func setup() {

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.navigationBar.barStyle = .blackOpaque
        navigationItem.title = "YOUR_TITLE_HERE"
        navigationController?.navigationBar.barTintColor = .black
        let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navigationController?.navigationBar.largeTitleTextAttributes = attributes
    }

要普遍更改颜色,此代码应位于 NavigationControllerviewDidLoad 函数中:

class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Status bar white font
        self.navigationBar.barStyle = UIBarStyle.Black
        self.navigationBar.tintColor = UIColor.whiteColor()
    }
}

要针对每个 ViewController 更改,您必须从 ViewController 引用 NavigationController ,并在该 ViewControllerviewWillAppear 函数中编写类似的行。


Swift 4.2版的Albert的答案-

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white]

要在情节提要板(Interface Builder检查器)中完成此工作

借助 IBDesignable ,我们可以为 UINavigationController Interface Builder检查器添加更多选项,并在情节 IBDesignable 对其进行调整。 首先,将以下代码添加到您的项目中。

@IBDesignable extension UINavigationController {
    @IBInspectable var barTintColor: UIColor? {
        set {
            navigationBar.barTintColor = newValue
        }
        get {
            guard  let color = navigationBar.barTintColor else { return nil }
            return color
        }
    }

    @IBInspectable var tintColor: UIColor? {
        set {
            navigationBar.tintColor = newValue
        }
        get {
            guard  let color = navigationBar.tintColor else { return nil }
            return color
        }
    }

    @IBInspectable var titleColor: UIColor? {
        set {
            guard let color = newValue else { return }
            navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: color]
        }
        get {
            return navigationBar.titleTextAttributes?["NSForegroundColorAttributeName"] as? UIColor
        }
    }
}

然后,只需在情节提要上设置UINavigationController的属性即可。







uinavigationbar