首页 文章

更改导航栏中“后退”按钮的颜色

提问于
浏览
126

我正在尝试将“设置”按钮的颜色更改为白色,但无法将其更改 .

我试过这两个:

navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()

但没有变化,它仍然看起来像这样:

enter image description here

如何将该按钮设为白色?

22 回答

  • 0

    你应该用这个:

    navigationController?.navigationBar.barTintColor = .purple
    navigationController?.navigationBar.tintColor = .white
    
  • 73

    你有一个选择隐藏你的后退按钮,并与你自己 . 然后设置它的颜色 .

    我这样做了:

    self.navigationItem.setHidesBackButton(true, animated: true)
    let backbtn = UIBarButtonItem(title: "Back", style:UIBarButtonItemStyle.Plain, target: self, action: "backTapped:")
    self.navigationItem.leftBarButtonItem = backbtn
    self.navigationItem.leftBarButtonItem?.tintColor = UIColor.grayColor()
    
  • 3

    对于Swift 2.0,要在AppDelegate.swift中使用以下内容更改 Navigation-bar tint colortitle textback button 色调更改

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
      // Override point for customization after application launch.
    
    
        //Navigation bar tint color change
    
        UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)
    
        //Back button tint color change
    
        UINavigationBar.appearance().barStyle = UIBarStyle.Default
        UINavigationBar.appearance().tintColor =  UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)
    
        //Navigation Menu font tint color change
    
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)
    
        UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
    
    
        return true
    }
    
  • 17

    它将通过 - (void)viewDidLoad中的这一行解决:

    self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
    
  • 37

    didFinishLaunchingWithOptions 中,在 didFinishLaunchingWithOptions 内使用此代码 .

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    UINavigationBar.appearance().tintColor = .white
    
    }
    
  • 4

    如果您已经在“设置”视图控制器中有后退按钮,并且您想要将“付款信息”视图控制器上的后退按钮颜色更改为其他内容,则可以在“设置”视图中执行此操作控制器准备这样的segue :

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
        if segue.identifier == "YourPaymentInformationSegue"
        {
            //Make the back button for "Payment Information" gray:
            self.navigationItem.backBarButtonItem?.tintColor = UIColor.gray               
        }
    }
    
  • 6
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    

    这适用于我,iOS 9.0

  • 2
    self.navigationController?.navigationBar.tintColor = UIColor.redColor()
    

    这个片段带来了魔力 . 而不是redColor,将其更改为您的愿望 .

  • 143

    在Swift 4中,您可以使用以下方法处理此问题:

    let navStyles = UINavigationBar.appearance()
    // This will set the color of the text for the back buttons.
    navStyles.tintColor = .white
    // This will set the background color for navBar
    navStyles.barTintColor = .black
    
  • 173

    设置 UINavigationBar.appearance().tintColor 的所有答案都与 UIAppearance.h 中的Apple文档冲突 .

    iOS7注意事项:在iOS7上,tintColor属性已移至UIView,现在具有UIView.h中描述的特殊继承行为 . 此继承的行为可能与外观代理冲突,因此现在不允许使用外观代理来设置tintColor .

    在Xcode中,您需要按命令单击要与外观代理一起使用的每个属性,以检查头文件并确保使用 UI_APPEARANCE_SELECTOR 注释该属性 .

    因此,通过外观代理为整个应用程序着色导航栏紫色以及 Headers 和按钮白色的正确方法是:

    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().barTintColor = .purple
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UIBarButtonItem.appearance().tintColor = .white
    

    请注意 UIBarButtonItem 不是 UIView 的子类,而是 NSObject . 所以它的 tintColor 属性不是 UIView 的继承 tintColor .

    不幸的是, UIBarButtonItem.tintColor 没有用 UI_APPEARANCE_SELECTOR 注释 - 但在我看来这是一个文档错误 . Apple Engineering在this radar的回应表明它得到了支持 .

  • 1

    Swift 4.2

    Change complete app theme

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            UINavigationBar.appearance().tintColor = .white
    
            return true
        }
    

    Change specific controller

    let navController = UINavigationController.init(rootViewController: yourViewController) 
    navController.navigationBar.tintColor = .red
    
    present(navController, animated: true, completion: nil)
    
  • 5

    Swift3 中,将“后退”按钮设置为 red .

    self.navigationController?.navigationBar.tintColor = UIColor.red
    
  • 0

    你可以像这样使用 . 把它放在 AppDelegate.swift 里面 .

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            // Override point for customization after application launch.
    
            UINavigationBar.appearance().translucent = false
            UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
            UINavigationBar.appearance().tintColor = UIColor.whiteColor()
            UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
    
            return true
        }
    
  • 4

    斯威夫特3

    override func viewDidLoad() {
         super.viewDidLoad()
    
     self.navigationController?.navigationBar.tintColor = UIColor.white
     }
    
  • 0

    让我们试试这段代码:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
    
        let navigationBarAppearace = UINavigationBar.appearance()
        navigationBarAppearace.tintColor = UIColor.whiteColor()  // Back buttons and such
        navigationBarAppearace.barTintColor = UIColor.purpleColor()  // Bar's background color
        navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]  // Title's text color
    
        self.window?.backgroundColor = UIColor.whiteColor()
        return true
    }
    
  • 3

    在swift 2.0中使用

    self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
    
  • 4

    不知道为什么没有人提到过这个...但我正在做我在_146425中所做的事......而且它没有用 . 然后我将我的代码放入 viewWillAppear ,这一切都奏效了 .

    上面的解决方案是更改单个barbuttonItem . 如果要更改代码中每个navigationBar的颜色,请遵循this answer .

    基本上使用 appearance() 更改类本身就像在应用程序中对该视图的所有实例进行全局更改 . 欲了解更多,请参阅here

  • 13

    您可以通过单击电路板上的空白区域并在右侧工具栏中选择“显示文件检查器”来更改故事板中的全局色调颜色,您将在工具栏的底部看到“全局色调”选项 .

  • 11

    此代码更改箭头颜色

    self.navigationController.navigationBar.tintColor = UIColor.whiteColor();
    

    如果这不起作用,请使用以下代码:

    self.navigationBar.barStyle = UIBarStyle.Black
    self.navigationBar.tintColor = UIColor.whiteColor()
    

    Swift 3 Notes

    UIColor.whiteColor() 和类似已被简化为 UIColor.white

    此外,许多以前隐式的选项已更改为显式,因此您可能需要:

    self.navigationController?.navigationBar =
    
  • 6

    在故事板中很容易设置:

  • 12

    AppDelegate.swift 中将以下代码添加到 didFinishLaunchingWithOptions 函数

    var navigationBarAppearace = UINavigationBar.appearance()
    
    navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
    navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade
    
    // change navigation item title color
    navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
    
  • 3

    Swift 3

    对于Swift 3,最受欢迎的答案是不正确的 .

    更改颜色的正确代码是:

    self.navigationController?.navigationBar.tintColor = UIColor.white
    

    如果要更改颜色,请将上面的UIColor.white更改为所需的颜色

相关问题