标签栏中的导航栏中的按钮UIViewController未显示

时间:2022-05-16 22:56:17

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)

流:导航控制器->标签栏控制器-> ViewController(想在这里放按钮)

I've tried adding it programmatically inside of ViewDidLoad:

我尝试过在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

我也尝试过使用故事板添加它,但它在运行时不会显示。知道为什么我不能在导航条中显示一个按钮吗

2 个解决方案

#1


3  

Try it like this instead:

试试这样:

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

#2


-1  

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

在viewWillAppear方法调用之后,从故事板或xib加载视图。以及viewWillAppear之前的viewDidLoad方法调用。

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.

导航条在viewDidLoad方法中是nil所以它们不会出现。所以在viewdidappear方法中编写代码,您的按钮就会出现。

#1


3  

Try it like this instead:

试试这样:

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

#2


-1  

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

在viewWillAppear方法调用之后,从故事板或xib加载视图。以及viewWillAppear之前的viewDidLoad方法调用。

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.

导航条在viewDidLoad方法中是nil所以它们不会出现。所以在viewdidappear方法中编写代码,您的按钮就会出现。