我以编程方式在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
}
我也试过删除外观设置仍然按钮不可见。
您可以在代码中删除self.navigationController?.
。
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(logoutButtonTapped))
}
不要使用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
}
希望您的问题能够通过此代码解决。谢谢。
let rightBarButton = UIBarButtonItem(image: UIImage(named: "test"), style: .plain, target: self, action: #selector(logoutButtonTapped))
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
现在这对我有用