I'm working on an app where my initial view controller is embedded in a Navigation Controller and Tab bar controller.
我正在开发一个应用程序,我的初始视图控制器嵌在导航控制器和标签栏控制器中。
In my app I have 3 different tabs, one tab is the settings view. This is a tableview with 5 buttons. every button will show the same view controller when it's touched.
在我的应用中有3个不同的标签,一个标签是设置视图。这是一个有5个按钮的tableview。每个按钮在触摸时都会显示相同的视图控制器。
When I touch on one of the buttons, I still have a nice green navigation bar, but my buttons do not work.
当我触摸其中一个按钮时,我仍然有一个漂亮的绿色导航条,但是我的按钮不能工作。
I tried dragging a Navigation Item into the view in my storyboard and then put a bar button item in it. I can see the button in my storyboard, but it won't show up in my app when I run it.
我尝试将导航项拖放到故事板中的视图中,然后在其中放入一个bar按钮项。我可以在故事板中看到这个按钮,但是当我运行它时它不会出现在我的应用程序中。
I also added the following code to my viewDidLoad():
我还向我的viewDidLoad()添加了以下代码:
let leftItem = UIBarButtonItem(title: "< Back", style: .Done, target: self, action: Selector("Save"))
let rightItem = UIBarButtonItem(title: "Save", style: .Plain, target: self, action: Selector("Save"))
//self.parentViewController?.navigationItem.leftBarButtonItem = leftItem // also doesn't work
navigationController?.navigationItem.leftBarButtonItem = leftItem
navigationController?.navigationItem.rightBarButtonItem = rightItem
But it won't have any effect.
但不会有任何效果。
I checked if the correct UIViewController was assigned to my view in the storyboard, did several Clean builds (CMD + Shift + K) and Rebuilds (CMD + B).
我检查了正确的UIViewController是否被分配给我在故事板中的视图,做了几个干净的构建(CMD + Shift + K)和重建(CMD + B)。
The following images are a screenshot of a part of my storyboard and a screenshot of my app at the view where the buttons won't show up.
下面的图片是我的故事板的一部分的截图,以及我的应用在视图的截图,在那里按钮不会出现。
EDIT
编辑
Added a new screenshot about the controls in the view. These do not work with or without the additional code in my viewDidLoad.
在视图中添加了关于控件的新屏幕截图。在我的viewDidLoad中,这些代码不能使用或不使用额外的代码。
5 个解决方案
#1
5
Delete your first navigation controller, second you dont need to add back button just use push segue, third try to use self.navigationItem.leftBarButtonItem(try adding self at the beginning.) *Original answer was a comment.
删除第一个导航控制器,第二,不需要添加后退按钮,只需使用push segue,第三,尝试使用self。navigationitem。leftBarButtonItem(试着在开头添加self) *最初的答案是注释。
#2
7
Instead of
而不是
navigationController?.navigationItem.leftBarButtonItem = leftItem
do
做
navigationItem.leftBarButtonItem = leftItem
Update I suggest you add them directly in the storyboard to your Viewcontroller
更新,我建议你直接在故事板中添加它们到你的视图控制器中
#3
2
I had two segues: one via a storyboard segue, and one programmatic. The storyboard segue showed the buttons as expected. The progammatic segue, however, showed the view without the buttons.
我有两个segue:一个是通过storyboard segue,另一个是程序化的。storyboard segue显示的按钮与预期的一样。然而,progammatic segue显示了没有按钮的视图。
For the programmatic segue, I was using:
对于程序化segue,我使用的是:
present(theNewVC, animated: true)
The buttons appeared and functioned correctly when I changed that to:
当我把按钮改成:
show(theNewVC, sender: self)
Per the documentation, present(...)
presents the view controller modally. Apparently a modal view controller doesn't get the nav bar buttons. I further confirmed this by changing my storyboard segue to Present Modally
- and the buttons disappeared there as well.
根据文档,present(…)以模态方式呈现视图控制器。显然模态视图控制器没有导航栏按钮。我通过更改我的storyboard segue以表示Modally来进一步证实了这一点——按钮也在那里消失了。
(Using Xcode 9, Swift 4)
(使用Xcode 9, Swift 4)
#4
0
This happened to me recently. I was doing what Ersin mentioned, but to no avail. Backtracked a little more and found out that this was the culprit:
这是最近发生在我身上的事。我做的是厄辛提到的事,但无济于事。再回溯一点,发现这就是罪魁祸首:
nav.pushViewController(notifications, animated: false) nav.pushViewController(interactionCreateOrEdit, animated: false)
导航。pushViewController(通知、动画:false)导航。pushViewController(interactionCreateOrEdit动画:假)
I changed it to
我改变了它
nav.setViewControllers([notifications, interactionCreateOrEdit], animated: false)
导航。setViewControllers([通知,interactionCreateOrEdit],动画:false)
and the UIBarButtonItems started appearing again.
UIBarButtonItems又开始出现了。
#5
-1
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[button setImage:[[UIImage imageNamed:@"BtnBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[button.imageView setTintColor:[UIColor whiteColor]];
[button addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
self.navigationItem.leftBarButtonItem = buttonItem;
this Code Write on ViewWillAppear method.
这段代码写在ViewWillAppear方法上。
#1
5
Delete your first navigation controller, second you dont need to add back button just use push segue, third try to use self.navigationItem.leftBarButtonItem(try adding self at the beginning.) *Original answer was a comment.
删除第一个导航控制器,第二,不需要添加后退按钮,只需使用push segue,第三,尝试使用self。navigationitem。leftBarButtonItem(试着在开头添加self) *最初的答案是注释。
#2
7
Instead of
而不是
navigationController?.navigationItem.leftBarButtonItem = leftItem
do
做
navigationItem.leftBarButtonItem = leftItem
Update I suggest you add them directly in the storyboard to your Viewcontroller
更新,我建议你直接在故事板中添加它们到你的视图控制器中
#3
2
I had two segues: one via a storyboard segue, and one programmatic. The storyboard segue showed the buttons as expected. The progammatic segue, however, showed the view without the buttons.
我有两个segue:一个是通过storyboard segue,另一个是程序化的。storyboard segue显示的按钮与预期的一样。然而,progammatic segue显示了没有按钮的视图。
For the programmatic segue, I was using:
对于程序化segue,我使用的是:
present(theNewVC, animated: true)
The buttons appeared and functioned correctly when I changed that to:
当我把按钮改成:
show(theNewVC, sender: self)
Per the documentation, present(...)
presents the view controller modally. Apparently a modal view controller doesn't get the nav bar buttons. I further confirmed this by changing my storyboard segue to Present Modally
- and the buttons disappeared there as well.
根据文档,present(…)以模态方式呈现视图控制器。显然模态视图控制器没有导航栏按钮。我通过更改我的storyboard segue以表示Modally来进一步证实了这一点——按钮也在那里消失了。
(Using Xcode 9, Swift 4)
(使用Xcode 9, Swift 4)
#4
0
This happened to me recently. I was doing what Ersin mentioned, but to no avail. Backtracked a little more and found out that this was the culprit:
这是最近发生在我身上的事。我做的是厄辛提到的事,但无济于事。再回溯一点,发现这就是罪魁祸首:
nav.pushViewController(notifications, animated: false) nav.pushViewController(interactionCreateOrEdit, animated: false)
导航。pushViewController(通知、动画:false)导航。pushViewController(interactionCreateOrEdit动画:假)
I changed it to
我改变了它
nav.setViewControllers([notifications, interactionCreateOrEdit], animated: false)
导航。setViewControllers([通知,interactionCreateOrEdit],动画:false)
and the UIBarButtonItems started appearing again.
UIBarButtonItems又开始出现了。
#5
-1
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[button setImage:[[UIImage imageNamed:@"BtnBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[button.imageView setTintColor:[UIColor whiteColor]];
[button addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
self.navigationItem.leftBarButtonItem = buttonItem;
this Code Write on ViewWillAppear method.
这段代码写在ViewWillAppear方法上。