How to reload Navigation bar - ios

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

Related

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;

Can not show the rightbar button - ios sdk

I had add image at my navigation bar and need to add the right bar refresh button.
But the right bar button can not show at the navigation after add.
Why?
Here the code add image at navigation bar:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"topbar.png"] forBarMetrics:UIBarMetricsDefault];
Here the add right bar button at viewDidLoad:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
NSArray *actionButtonItems = #[refreshItem];
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
You should not add to self.navigationcontroller.
Do something like this will resolve your problem.
self.navigationItem.rightBarButtonItem = refreshItem;
Try with following code
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
self.navigationItem.rightBarButtonItem = refreshItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightButtoneClicked:)];
self.navigationItem.rightBarButtonItem = rightButton;
Right this code in viewDidLoad and you get the refresh button on right side.
Replace this
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
With:
self.navigationItem.rightBarButtonItems = actionButtonItems;

Any idea why the custom nav bar back button image isn't displaying with this code?

Any idea why the custom nav bar back button image isn't displaying with this code?
instead of showing the uiimage it's showing the "< Back" default iOS button in text only
[self.navigationController setNavigationBarHidden:NO animated:NO];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"freccia.png"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:#selector(backBtnPress)];
could it be this parameter? UIBarButtonItemStylePlain
thanks
use this code:
UIImage *imageBack = [UIImage imageNamed:#"yourImage.png"];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.bounds = CGRectMake(0, 0, imageBack.size.width, imageBack.size.height);
[btnBack setBackgroundImage:imageBack forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = barButtonBack;
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
You usually set the back button in the parent view controller. However, it might just be easier to use leftBarButtonItem instead of backBarButtonItem.
you can solve this problem by two ways
1. use leftBarButtonItem instead of backBarButtonItem
2. try the below code
self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];

Add Back Button back into navigationController after adding custom button

I'm trying to implement a navigationcontroller that has a uitableview in it. This tableview should be editable, and to add rows to the table the user should press a plus button on the top bar on the screen. However, when I add a uibarbuttonitem and set it to the left side, the back button doesn't re-appear? how would i make it reappear?
The edit button:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButtonPressed)];
[[self navigationItem] setRightBarButtonItem:button];
The editButtonPressed method:
[_actionList setEditing:TRUE];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAction)];
[[self navigationItem] setLeftBarButtonItem:addButton animated:TRUE];
editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editButtonDone)];
[[self navigationItem] setRightBarButtonItem:editButton animated:TRUE];
[self disableButtons];
The editButtonDone method:
[_actionList setEditing:FALSE];
editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButtonPressed)];
[[self navigationItem] setRightBarButtonItem:editButton];
[self enableButtons];
I'm a beginner so sorry if this is dumb.
Try resetting left bar button when you leave edit mode.
[[self navigationItem] setLeftBarButtonItem:nil animated:YES];
try this:
[yourNavBar pushNavigationItem:self.navigationItem animated:NO];
Hope it helps

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