简体   繁体   中英

Button in Navigation Bar in Tab Bar UIViewController not showing

I can't seem to get any buttons to appear in my navigation bar.

Flow: Navigation Controller -> Tab bar Controller -> ViewController (want to put button here)

I've tried adding it programmatically inside of ViewDidLoad:

let navigationBar = navigationController!.navigationBar
    navigationBar.tintColor = UIColor.blackColor()

let leftButton =  UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

    navigationItem.leftBarButtonItem = leftButton
    navigationItem.rightBarButtonItem = rightButton

I've also tried adding it using the storyboard but it doesnt show during runtime. Any idea why I can't get a button to appear in the navigation bar

试试这样:

self.tabBarController?.navigationItem.leftBarButtonItems = [leftButton]

views from storyboard or xib load after viewWillAppear method call. and viewDidLoad method call before viewWillAppear.

so navigation bar is nil during viewDidLoad method so they are not appear.so write you code in viewdidappear method and your buttons will be appeared.

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