ios 导航栏中未显示栏按钮项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33696523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:14:08  来源:igfitidea点击:

Bar button item not shown in navigationBar

iosswiftuinavigationbaruinavigationitem

提问by Jules

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.

我尝试将导航项拖到我的故事板中的视图中,然后在其中放置一个条形按钮项。我可以在我的故事板中看到该按钮,但是当我运行它时它不会显示在我的应用程序中。

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 分配给我在故事板中的视图,做了几次 Clean builds (CMD + Shift + K) 和 Rebuilds (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 orwithout the additional code in my viewDidLoad.

添加了有关视图中控件的新屏幕截图。无论是否有我的 viewDidLoad 中的附加代码,这些都不起作用。

Screenshot of my Storyboard

我的故事板的屏幕截图

Screenshot of my app where the buttons should show up

我的应用程序的屏幕截图,按钮应该显示在那里

Screenshot of the the setup of the controls in my view

我的视图中控件设置的屏幕截图

采纳答案by Ersin Sezgin

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。) *原始答案是评论。

回答by MarkHim

Instead of

代替

navigationController?.navigationItem.leftBarButtonItem = leftItem

do

navigationItem.leftBarButtonItem = leftItem

UpdateI suggest you add them directly in the storyboard to your Viewcontroller

更新我建议您将它们直接在情节提要中添加到您的 Viewcontroller

回答by iDev

I also ran into same issue.

我也遇到了同样的问题。

In iOS 11 it's working fine but for iOS 10 or less it was not showing UIBarButtonItems which added programatically.

在 iOS 11 中它运行良好,但对于 iOS 10 或更低版本,它没有显示以编程方式添加的 UIBarButtonItems。

Please assign frame to UIBarButtonItem.

请将框架分配给 UIBarButtonItem。

let button =  UIButton(type: .custom)
button.setImage(UIImage(named: "img_name"), for:.normal)
button.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
button.frame = CGRect(x:0, y:0, width:32, height:32)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton

回答by leanne

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 按预期显示按钮。然而,编程转场显示了没有按钮的视图。

For the programmatic segue, I was using:

对于程序化转场,我使用的是:

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(...)以模态方式呈现视图控制器。显然,模态视图控制器没有导航栏按钮。我通过将我的故事板 segue 更改为Present Modally-进一步证实了这一点,按钮也消失了。

(Using Xcode 9, Swift 4)

(使用 Xcode 9、Swift 4)

回答by saraman

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 方法上。

回答by Michael McKenna

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:

这最近发生在我身上。我正在做 Ersin 提到的事情,但无济于事。回溯了一点,发现这是罪魁祸首:

nav.pushViewController(notifications, animated: false) nav.pushViewController(interactionCreateOrEdit, animated: false)

nav.pushViewController(notifications, animated: false) nav.pushViewController(interactionCreateOrEdit, animated: false)

I changed it to

我把它改成

nav.setViewControllers([notifications, interactionCreateOrEdit], animated: false)

nav.setViewControllers([notifications, interactionCreateOrEdit], animated: false)

and the UIBarButtonItems started appearing again.

并且 UIBarButtonItems 再次开始出现。