简体   繁体   English

设置后退按钮颜色

[英]Set back button color

I have set all bar button items, including back buttons, with... 我已将所有条形按钮项目(包括后退按钮)设置为...

UIBarButtonItem.appearance.tintColor = UIColor.red

However, I want to override the back button color on one page but I cannot find a reference to a back button. 但是,我想覆盖一页上的后退按钮颜色,但是找不到对后退按钮的引用。 All the references with the right sounding name are null. 具有正确冠冕堂皇名称的所有引用均为空。

Why are they all null? 为什么它们都为空? Where does the back button come from? 后退按钮从哪里来?

Try it without using the appearance proxy. 不使用appearance代理尝试它。 Set the main window's tintColor property to your default color. 将主窗口的tintColor属性设置为默认颜色。 Then in your specific view controller's viewWillAppear / viewWillDisappear set / reset the specific color: 然后在您的特定视图控制器的viewWillAppear / viewWillDisappear设置/重置特定颜色:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow? {
        didSet {
            // set your default tint color
            window?.tintColor = .red
        }
    }

}

class SpecificViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.tintColor = .magenta
    }

    override func viewWillDisappear(_ animated: Bool) {
        navigationController?.navigationBar.tintColor = nil
        super.viewWillDisappear(animated)
    }

}

Results in: 结果是:

结果

What you should rather be doing is setting the navigationBar.tintColor . 您应该做的是设置navigationBar.tintColor

UINavigationBar.appearance.tintColor = .red

This will apply the color to every navigation bar. 这会将颜色应用于每个导航栏。 So you have to set and reset it again in the view controller you want to change the color. 因此,您必须在要更改颜色的视图控制器中再次进行设置和重置。

In viewWillAppear : viewWillAppear

self.navigationContoller?.navigationBar.tintColor = .blue

In viewWillDisappear : viewWillDisappear

self.navigationController?.navigationBar.tintColor = .red

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM