导航栏按钮不显示在导航栏中

问题描述 投票:1回答:3

我以编程方式在rightBarButton上设置了navigationBar,但这并未显示,尽管标题显示正常。

这是我的代码形式AppDelegate

    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]

这里是我的viewController的代码,我试图添加rightBarButton

override func viewDidLoad()
{
    super.viewDidLoad()

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

我也试过删除外观设置仍然按钮不可见。

enter image description here

ios swift uinavigationbar uibarbuttonitem
3个回答
0
投票

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

override func viewDidLoad() {
    super.viewDidLoad()

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

0
投票

不要使用self.navigationController?.navi ...而不是使用self.navigationItem.rightBarButton ...

override func viewDidLoad()
{
    super.viewDidLoad()

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

希望您的问题能够通过此代码解决。谢谢。


0
投票
     let rightBarButton = UIBarButtonItem(image: UIImage(named: "test"), style: .plain, target: self, action: #selector(logoutButtonTapped))

    self.navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton

现在这对我有用

© www.soinside.com 2019 - 2024. All rights reserved.