As you see in the images, the top picture is created automatically when navigation controller is pushed
If I try to create one like this it will appear like the bottom picture.
How can I programmatically create a Back Button like the top picture?
Here is my code to create the Done button
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(backtohome)];
UIBarButtonItem *theBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = theBackButton;
[theBackButton release];
Related
I want to add two buttons two navigation bar on right hand side one for settings and one for login but problem is that only one button i have searched comes on right that is edit is there any other way in which we can make two button and give them desired title.
UIBarButtonItem *addAttachButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(#"Send") style:UIBarButtonItemStyleBordered target:self action:#selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = #[addAttachButton,sendButton];
Here is the code I am using in My UIViewController's implementation file (.m file)
-(void)viewDidLoad{
[super viewDidLoad];
//back button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#" Back " style:UIBarButtonItemStyleBordered target:self action:#selector(backTo:)];
//Optional: if you want to add space between the back & login buttons
UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpaceBarButtonItem.width = 12;
//login button
UIBarButtonItem *signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:#" SIGN IN " style:UIBarButtonItemStyleBordered target:self action:#selector(signInUser)];
//add all buttons to right side
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:backButton, fixedSpaceBarButtonItem,signIn_BarButton, nil];
}
now here are the metthods for both button clicks
-(IBAction)backTo:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
-(void) signInUser{
//handle your sigin logic here
}
Please! let me if you need more help!
How can I create an UIBarButtonItem without any border, but just a label inside?
EDIT
I tried something like this with no luck:
UIBarButtonItem* langButton = [[UIBarButtonItem alloc] initWithTitle:#"EN"
style:UIBarButtonItemStylePlain target:self action:#selector(changeLang)];
self.navigationItem.leftBarButtonItem = langButton;
[langButton release];
Do the same as the following guy:
https://stackoverflow.com/a/6817554/59198
But change it so instead of an image button, you'll make a standard textual button.
I have a UITableViewController A that is pushing UITableViewController B onto the stack.
In A i have the code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonClicked:)];
backButtonClicked: is also implemented.
B has the title Trending, but when I click it, it doesn't ever reach backButtonClicked:
Why is this?
Try either setting the delegate:
[navigationController setDelegate:self];
Or using the left button. Sometimes the back button doesn't work with certain views and I have had to use the left button instead:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
Also, you can try setting B's button item instead of A:
viewControllerB.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
In the Xcode documentation, it states that the backBarButtonItem target and action should be set to nil. So even if you do set it, it's probably a good bet that it will be ignored. You could check out the link below to add custom behaviour to the back button.
Custom Action on Back Button Item
Or you could just do the following in viewControllerB:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Trending"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonClicked:)] autorelease];
Then also add this to viewControllerB
- (void)backButtonClicked:(id)sender {
[[[self.navigationController viewControllers] objectAtIndex:0] backButtonClicked:sender];
[self.navigationController popViewControllerAnimated:YES];
}
The above method will find the RootViewController and send it the backButtonClicked message. It will then pop the current view controller, which should allow you to emulate the backBarButtonItem. Also you can change which view controller you want to send the message by changing the value in the objectAtIndex method.
try not backBarButtonItem but leftBarButtonItem instead
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
self.navigationItem.hidesBackButton=YES;
for me it works like a charm. And don't forget that you didn't release this button - and it can cause memory leak. So you can add autorelease when you assign you button or make it like this
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
self.navigationItem.leftBarButtonItem = myBackButton;
[myBackButton release];
self.navigationItem.hidesBackButton=YES;
i have problem that i have 3 views,
i goto first view to 2nd and want to change mine back button title
i write this code in viewDidLoad of 2nd View but its not working :(
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
go to 1st view controller and add the following code before you push the viewController,
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Inv Menu" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
How would I be able to set the position of a UIBarButtonItem? Like, I would like to set it to be on either the very right of a UIToolbar or the very left depending on a state.
Thank you.
You do not set directly the position of a UIBarButtonItem in an UIToolbar. Instead you defined the items' order and put flexible space on the left or on the right.
What you can do is:
Create the UIBarButtonItem you want to place (button 1).
Create an UIBarButtonItem of type UIBarButtonSystemItemFlexibleSpace (button 2).
If you want to put the button on the left, create an array with (button 1) and (button 2) and pass it to the UIToolbar by using the setItems:animated: method.
If you want to put the button on the right, create an array with (button 2) and (button 1) and pass it to the UIToolbar by using the setItems:animated: method.
I hope it helps you...
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //<
UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //>
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneClicked:)];
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] ;
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES];
Link explains Bar Button Item Methods and Arguments
https://developer.apple.com/library..... Use Fake Item to get exact pinch location on Button...