简体   繁体   中英

Navigation bar button doesn't show in navigationbar

I set rightBarButton on navigationBar programmatically, but that doesn't show, though the title is showing alright.

Here is my code form 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]

And here is code from my viewController where I am trying to add 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
}

I have also tried removing appearance settings still the button is not visible.

在此处输入图片说明

You can remove self.navigationController?. in your code.

override func viewDidLoad() {
    super.viewDidLoad()

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

Don't use self.navigationController?.navi... instead of that use 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
}

Hope your problem will be solved by this code. Thankyou.

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

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

now this is works for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM