Right UIBarButton not shown - ios

As simple as this code is :
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Inizializzazione barra di navigazione
[[self navigationController] setNavigationBarHidden:NO];
UINavigationItem* a = [self navigationItem];
[a setTitle:#"SOME TITLE"];
UIImage *background = [UIImage imageNamed:#"header.png"];
CGSize newSize;
newSize.height=100;
[self.navigationController.navigationBar setBackgroundImage:background
forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Homefgh"
style:UIBarButtonItemStyleBordered
target:self action:#selector(home)];
self.navigationController.navigationItem.rightBarButtonItem = rightButton;
}
but the button is not shown (on the right).
I tried also to put the code in the viewDidLoad.
The viewWillAppear is inside a UIView pushed on top of the main UINavigationController.
The strange thing is that the background image is correctly shown.

You should be able to do something as simple as this to get it to show up:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Homefgh"
style:UIBarButtonItemStyleBordered
target:self action:#selector(home)];
self.navigationItem.rightBarButtonItem = rightButton;

Changeself.navigationController.navigationItem.rightBarButtonItem = rightButton; to self.navigationItem.rightBarButtonItem = rightButton;

- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
}
Remember to put it in viewDidLoad instead of viewWillAppear.

Add this in your code
self.navigationItem.rightBarButtonItem = rightButton;
Instead of this code
self.navigationController.navigationItem.rightBarButtonItem = rightButton;
And it will be displayed.

Related

NavigationBar changed in iOS10

I use the following code to setup left & right bar item,
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"nav-back"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(leftItemTapped:)];
Now it stopped working in iOS 10, the items are not "clickable"
Any one know how to fix that?
Write this code in viewWillAppear, and it should work properly...
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"nav-back"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(leftItemTapped:)];
}
Try this code:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"nav-back"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(leftItemTapped:)];
}
- (IBAction)leftItemTapped:(id)sender{
NSLog(#"Navigation");
}
Output:
2016-09-15 15:30:00.972 test[34398:248335] Navigation

How can I add a button to a navigation bar programmatically in xcode

I'm trying to add button to navigation bar i want to add the button without any action any help please?
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc]
initWithTitle:#"Your Button"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(methodName:)];
self.navigationItem.rightBarButtonItem = yourButton;
[yourButton release];
And then method
-(IBAction)methodName {
// your code here
}
You should add UIBarButtonItem to Nav bar instead of UIButton. T do that you can call this:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.rightBarButtonItem = button;
UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:#"Custom" style:UIBarButtonItemStylePlain target:self action:#selector(customBtnPressed)];
[self.navigationItem setRightBarButtonItem:customBtn];
///// called event
-(IBAction)customBtnPressed:(id)sender
{
//Your code here
}
if UIButton is you really want, try this.
UIButton *btnSample = [[UIButton alloc] initWithFrame:btnFrame];
btnSample.backgroundColor = [UIColor blueColor];
UIBarButtonItem *barBtn_cart = [[UIBarButtonItem alloc] initWithCustomView:btnSample];
self.navigationItem.rightBarButtonItem = btnSample;

How do I add a right button to a UINavigationController that pushes another view controller?

I have a view controller connected to a navigation controller. I want to add a right button to the navigation bar on the top that will push another view controller. If I add the right button with:
UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:#"Map" style:UIBarButtonItemStylePlain target:self action:[self forward]];
self.navigationItem.rightBarButtonItem = Button;
the button appears. However, if I add the following lines to the forward method, the button disappears again:
MapViewController *viewController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:#"map"];
[self.navigationController pushViewController:viewController animated:YES];
What is the correct way of doing this?
there is a problem with the line action:[self forward]] while declaring UIBarButtonItem in your code...change it to #selector(forward) or try this
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(showNextVC)];
self.navigationItem.rightBarButtonItem = nextButton;
and in showNextVC:
-(void) showNextVC {
MapViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"map"];
[self.navigationController pushViewController:viewController animated:YES];
}
Try this code
UIImage *rightBtnImage = [UIImage imageNamed:#"bulb-plus.png"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:rightBtnImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, 26, 35);
[rightButton addTarget:self
action:#selector(rightBarButtonAction)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
IBAction method for rightBarbutton
- (void)rightBarButtonAction {
[self performSegueWithIdentifier:#"IdeaDetailSegue" sender:self];
}
Hope this helps.

How to reload Navigation bar

In UITableView I configure self.navigationItem.leftBarButtonItem with a button. Now I want to reload this button to update text on this button, but it's on Navigation bar. I can update UITableView by
[self.tableView reloadData]
but How to update Navigation bar?
Update: I'm NOT init the title of button, but reload it. So any init code (such as self.navigationItem.leftBarButtonItem.title = #"balabala") doesn't work, because it does NOT take effect immediately.
Just change the text on the leftBarButtonItem?
self.navigationItem.leftBarButtonItem.title = #"Back"; // or whatever text you want
Try this
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(OnClick:)];
self.navigationItem.leftBarButtonItem = btnBack;
Hope this helps you..
EDIT :-
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithTitle: #"Title" style: UIBarButtonItemStyleBordered target:self action:#selector(OnClick:)];
[[self navigationItem] setBackBarButtonItem: btnBack];
Did you try setting the text, using
self.navigationItem.leftBarButtonItem.title
You need to use the following statement:
self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];
In other words you need the setLeftBarButtonItem:animated message instead of the standard one. This will reload the button text with animation. Same applies for the back button if that is what you are interested in changing at some point.
#interface Myviewcontroller : UIViewController
{
UIBarButtonItem *barBtnBack;
UIButton *btnQuestion;
}
//ViewdidLoad
btnQuestion = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[btnQuestion setBackgroundImage:[UIImage imageNamed:#"question-mark2x"] forState:UIControlStateNormal];
[btnQuestion addTarget:self action:#selector(goBacktomain)
forControlEvents:UIControlEventTouchUpInside];
barBtnBack =[[UIBarButtonItem alloc] initWithCustomView:btnQuestion];
[self.navigationItem setLeftBarButtonItem:barBtnBack];
note- change btnQuestion title when you want to change navigationnar button title
If you want to change the NavigationBar title, try 1 of these:
[self.navigationController.navigationItem setTitle:#"Title"];
[self.navigationItem setTitle:#"Title"];
[self setTitle:#"Title"];
If you wanna change button titles, put this in the same class:
- (void)setLeftBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setLeftBarButtonItem:leftButton];
}
- (void)setRightBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setRightBarButtonItem:rightButton];
}
Examples to set button title and actions:
// Change button titles and actions any time
[self setLeftBarButtonTitle:#"Back" action:#selector(goBack)];
[self setRightBarButtonTitle:#"Something" action:#selector(doSomething:)];
// Change button titles with no actions
[self setLeftBarButtonTitle:#"Nothing" action:nil];
[self setRightBarButtonTitle:#"Nothing" action:nil];
// Remove buttons from navigation bar
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:nil];
Try this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"your_title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
I solved it in Swift, but if the keyboard is shown, it hides.
navigationController?.loadView()

Bar button not receiving touch

I have 2 bar buttons on the tool bar , one is not working and the other is working. Some times when we touch the tip of the first bar button it will work, I think this has to be a problem with flexible space between them.
I couldn't find any possible solution, any help will be appreciated!
I'm pasting the code below
editbutton is the first button and mybutton is the second button
-(void) rightButtonToolbar:(UIBarButtonItem*)menuButton{
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,TOOLBAR_WIDTH ,44)];
NSMutableArray *barButtons = [[NSMutableArray alloc]init];
UIBarButtonItem *flexiSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barButtons addObject:flexiSpace];
if (showEditButton)
{
NSLog(#"showeditbutton");
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editOffer)];
[barButtons addObject:editButton];
[editButton release];
}
UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc] initWithTitle:#"Kopiren" style:UIBarButtonSystemItemAction target:self action:#selector(copyOffer)];
[barButtons addObject:myButton2];
if (menuButton != nil) {
menuButtonShown=YES;
[barButtons addObject:menuButton];
}
else {
menuButtonShown=NO;
}
[toolbar setItems:barButtons animated:YES];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithCustomView:toolbar];
[self.navigationItem setRightBarButtonItem:rightBarButton animated:YES];
[barButtons release];
[flexiSpace release];
[rightBarButton release];
[toolbar release];
[myButton2 release];
}

Resources