我以编程方式将rightBarButton设置为navigationBar,但是没有显示,尽管标题显示正常。
这是我的代码表:

    UINavigationBar.appearance().barTintColor        = APP_COLOR
    UINavigationBar.appearance().tintColor           = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font:Constants.APP_REGULAR_FONT_WITH_SIZE(size: 18), NSAttributedString.Key.foregroundColor : UIColor.white]

这里是我的AppDelegate代码,我正试图添加viewController
override func viewDidLoad()
{
    super.viewDidLoad()

    let rightBarButton = UIBarButtonItem(image: UIImage(named: "logout"), style: .plain, target: self, action: #selector(logoutButtonTapped))
    self.navigationController?.navigationItem.rightBarButtonItem = rightBarButton
}

我也试过删除外观设置,但按钮仍然不可见。
ios - 导航栏按钮未显示在导航栏中-LMLPHP

最佳答案

您可以删除代码中的self.navigationController?.

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(logoutButtonTapped))
}

10-08 07:46