[英]Navigation bar button doesn't show in navigationbar
我以編程方式在navigationBar
上設置了rightBarButton
,但這沒有顯示,盡管標題顯示正常。
這是我的代碼形式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]
這是我試圖添加rightBarButton
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
}
我也嘗試過刪除外觀設置,但該按鈕仍然不可見。
您可以刪除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
現在這對我有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.