Leftbarbuttonitem not showing in navigation bar

I am developing an iOS application and am having problems using the image as an element of the left button on the navigation bar. I tried this as follows:

UIImage *backButtonImage = [UIImage imageNamed:@"backbuttonCB"]; CGRect buttonFrame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height); UIButton *backButton = [[UIButton alloc] initWithFrame:buttonFrame]; [backButton setImage:backButtonImage forState:UIControlStateNormal]; UIBarButtonItem *backBarButtonItem= [[UIBarButtonItem alloc] initWithCustomView: backButton]; self.navigationItem.leftBarButtonItem = backBarButtonItem; 

When the application starts, the button on the panel is never displayed.

Then I tried to apply this other method as follows.

 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backbuttonCB"] style:UIBarButtonItemStylePlain target:nil action:@selector(methodname)]; 

This method really worked. However, the displayed image was painted blue and did not look the way it was intended. Changing the hue color of this image did not help.

Any idea how I can solve this problem?

EDIT: More info if this helps. This is the first view on the navigation stack. The navigation stack is displayed modally, i.e. There is a previous view controller, and there is a modal indent between the previous view controller and the navigation controller. This is the first view on the navigation stack.

EDIT: PROBLEM FIXED. I think it was an error in xcode, because when I restarted xcode and tested it using the actual device instead of the emulator, it worked fine. However, it still does not work on the emulator.

+12
ios cocoa-touch uinavigationbar uibarbuttonitem
source share
7 answers

Try setting this parameter to leftBarButtonItem. When presented modally, this is not displayed.

self.navigationController?.navigationBar.topItem.leftBarButtonItem =

+26
source share

Calling myButton.sizeToFit() before assigning leftBarButtonItem helped me.

  let cancelButton = UIButton() cancelButton.backgroundColor = .red cancelButton.setTitle("Common/Cancel".localized, for: .normal) cancelButton.sizeToFit() //!!! let cancelBarButton = UIBarButtonItem(customView: cancelButton) navigationItem.leftBarButtonItem = cancelBarButton 
+13
source share

Try using this code,

 self.navigationItem.hidesBackButton=YES; UIImage *buttonImage = [UIImage imageNamed:@"Back"]; UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; [aButton setImage:buttonImage forState:UIControlStateNormal]; aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height); [aButton addTarget:self action:@selector(backBtnTapped) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton]; self.navigationItem.leftBarButtonItem = backButton; 
+6
source share

I had the same problem. To see the button, I had to change the hue color, here is the code in Swift:

 self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image:UIImage.init(named: "btn_back_default")!, style:UIBarButtonItemStyle.Plain, target: self, action: Selector("back")) self.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor() 
+4
source share

Replace your code to initialize the button as customType as follows:

 UIImage *backButtonImage = [UIImage imageNamed:@"backButtonCB"]; CGRect buttonFrame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height); UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; [backButton setFrame:buttonFrame]; [backButton setImage:backButtonImage forState:UIControlStateNormal]; UIBarButtonItem *backBarButtonItem= [[UIBarButtonItem alloc] initWithCustomView: backButton]; self.navigationItem.leftBarButtonItem = backBarButtonItem; 

Hope this helps .... :)

0
source share

Add a navigation item to the Views storyboard and create an output for it in the ViewController. Then use a socket instead of self.navigationController?.navigationItem to reference leftBarButtonItem.

0
source share

Just forget the navigation bar when presenting the view controller and use the regular button somewhere else, at the bottom of the screen. (Not a button that looks like a navigation element.) This is what Android is doing better: the back button is closer to your finger.

-one
source share

All Articles