簡體   English   中英

導航欄按鈕未顯示在導航欄中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM