Bar button item not shown in navigationBar

2019-02-21 13:16发布

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.

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():

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).

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.

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

6条回答
再贱就再见
2楼-- · 2019-02-21 13:52

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.

Please assign frame to 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

查看更多
Ridiculous、
3楼-- · 2019-02-21 14:13

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.

查看更多
孤傲高冷的网名
4楼-- · 2019-02-21 14:13

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.

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.

(Using Xcode 9, Swift 4)

查看更多
不美不萌又怎样
5楼-- · 2019-02-21 14:15

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)

I changed it to

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

and the UIBarButtonItems started appearing again.

查看更多
干净又极端
6楼-- · 2019-02-21 14:16
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.

查看更多
聊天终结者
7楼-- · 2019-02-21 14:17

Instead of

navigationController?.navigationItem.leftBarButtonItem = leftItem

do

navigationItem.leftBarButtonItem = leftItem

Update I suggest you add them directly in the storyboard to your Viewcontroller

查看更多
登录 后发表回答