iOS 8中的NavigationBar栏,色调和标题文本颜色

 campionezhao 发布于 2022-12-19 21:22

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

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

}

在此输入图像描述

9 个回答
  • 我喜欢亚历克斯的回答.如果你想快速尝试一下,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
    }
    

    在此输入图像描述

    2022-12-19 21:23 回答
  • 要普遍地改变颜色,此代码应坐在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你就必须引用NavigationControllerViewController写在类似线ViewControllerviewWillAppear功能.

    2022-12-19 21:24 回答
  • 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]
    

    对于NSAttributedStringKey,文档说:

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

    2022-12-19 21:24 回答
  • //在Swift 4中

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

    2022-12-19 21:24 回答
  • 要在objective-c中工作,我必须viewWillAppear在我的CustomViewController中添加以下行.

    [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
    

    2022-12-19 21:25 回答
  • 如果要为整个应用程序设置色调颜色和条形颜色,可以将以下代码添加到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
    `
    

    导航barTintColor和tintColor已设置

    2022-12-19 21:27 回答
  • 在storyboard中完成此工作(Interface Builder Inspector)

    借助于IBDesignable我们,我们可以为Interface Builder Inspector添加更多选项,UINavigationController并在故事板上进行调整.首先,将以下代码添加到项目中.

    @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
            }
        }
    }
    

    然后只需在storyboard上设置UINavigationController的属性.

    在此输入图像描述

    2022-12-19 21:27 回答
  • 在Swift 4.1和Xcode 9.4.1中

    self.navigationItem.title = "your name"
    let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
    navigationController?.navigationBar.titleTextAttributes = textAttributes
    

    2022-12-19 21:29 回答
  • 更新了swift 4

    override func viewDidLoad() {
        super.viewDidLoad()
            self.navigationController?.navigationBar.tintColor = UIColor.blue
            self.navigationController?.navigationBar.barStyle = UIBarStyle.black
    }
    

    2022-12-19 21:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有