Can't change back bar button text - ios

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.
I tried the following which didn't work:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
Thanks.

Try with This code
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"NewTitle"
style:UIBarButtonItemStylePlain
target:[self navigationController]
action:#selector(popViewControllerAnimated:)

Try with this code:
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:#"customBack" forState:UIControlStateNormal];
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
self.navigationItem.leftBarButtonItem = aLeftBarButtonItem;
For iOS 7 use below piece of code:
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:#"customBack" forState:UIControlStateNormal];
UIImage *backArrow = [UIImage imageNamed:#"backArrow"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:backArrow];
[imageView setFrame:CGRectMake(kScreenOrigin, topMargin, backArrow.size.width, backArrow.size.height)];
[aCustomBackButton addSubview:imageView];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8.0;
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
self.navigationItem.leftBarButtonItems = #[aNegativeSpacer, aLeftBarButtonItem];

Related

Programmatically add a back button to a UINavigationBar?

I am using the following code to achieve a navigation bar in my app. (my app was crashing when I used a push segue so I need a modal segue meaning the nav bar is hidden after the modal segue is called)
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];
Does any one know any methods I can use with the above code to achieve back button functionality in the nav bar??
Use below code to add back button on left side of navigation bar.Add UIBarButtonItem on Navigation bar.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnClicked:)];
self.navigationItem.leftBarButtonItem = backButton;
Use below code to add back button on left side
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];
UINavigationItem *item = [[UINavigationItem alloc]
init];
navbar.items= #[item];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnClicked:)];
item.leftBarButtonItem = backButton;
BackButton is better than LeftButton I think:
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] init];
[desVC.navigationItem setBackBarButtonItem:backBtn];
Add Back Button With Image :
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
UIImage *backImage = [UIImage imageNamed:#"backBtn"];
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-15];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
Regarding the back button, the proper way is not to create your own.
The UINavigationItem class has proper support for this:
func setHidesBackButton(_:animated:)
var leftItemsSupplementBackButton: Bool
So, if you want to add a custom button on the left side, just set leftItemsSupplementBackButton to true.
Or call
self.navigationItem.setHidesBackButton(false, animated:false)

Spacing between custom type right bar button items

I have four right bar button items added in a navigation bar.
UIButton *replybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[replybutton setImage:[UIImage imageNamed:#"reply.png"] forState:UIControlStateNormal];
[replybutton addTarget:self action:#selector(replyAction:)forControlEvents:UIControlEventTouchUpInside];
[replybutton setFrame:CGRectMake(0, 0, 30, 28)];
UIBarButtonItem *replyBarButton = [[UIBarButtonItem alloc] initWithCustomView:replybutton];
// there are 3 more buttons like this..
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:replyBarButton, replyAllBarButton, forwardBarButton, deleteBarButton, nil];
I want to give some extra spacing between these four bar button custom type items. I tried imageInsets,
replyBarButton.imageInsets = UIEdgeInsetsMake(0.0, 40.0, 0.0, 0.0);
but it doesn't give spacing. Could someone advise me, how can i give extra spacing between four right bar custom type button items?
You can use UIBarButtonSystemItemFlexibleSpace or UIBarButtonSystemItemFixedSpace.
Here is an example:
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(replyAction:) forControlEvents:UIControlEventTouchUpInside];
[button1 setFrame:CGRectMake(0, 0, 30, 28)];
// other buttons are created the same way
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView:button3];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:button4];
UIBarButtonItem *space1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *space3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.navigationItem.rightBarButtonItems = #[item1, space1, item2, space2, item3, space3, item4];
In this example, you are adding flexible spaces between every item. This will automatically space your items evenly.
If you want, you can also define the space:
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpace.width = 40.0 // adjust this

Custom Back Button for Navigation Bar

I made a category of UINavigationBar as follow:
UINavigationBar (UINavBar_Category)
I want to create custom back button using this.
I can able to set image by following code:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Problem can not able to change title of back button.
My try
- (void)didMoveToSuperview
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:lang(#"btnBack") style:UIBarButtonItemStylePlain target:nil action:nil];
self.topItem.backBarButtonItem=backButton;
}
Help me to solve this!
Thanks in advance
Try this code instead.. Here a button is first created , its title and image is set and then it is set as left bar button item of navigation controller.
UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 29)];
[backBarButton setTitle:#" Log Out" forState:UIControlStateNormal];
[backBarButton setImage:[UIImage
imageNamed:#"leftarrow_ipad.png"] forState:UIControlStateNormal];
[backBarButton addTarget:self action:#selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBarButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
Try this
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if([self.navigationController.viewControllers objectAtIndex:0] != self)
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 26)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal]; // some image
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = TRUE;
self.navigationItem.leftBarButtonItem = barBackItem;
}
}
-(void)popViewControllerWithAnimation
{
[self.navigationController popViewControllerAnimated:YES];
}

Fading out custom navbaritem with iOS 7.0

I have been working with iOS 7 and in one of my navigation controller and for some reason I had to use custom back button (with back arrow image and text next to it) as leftBarButtonItem. Now, in iOS 7 when an action sheet or alert is shown in the screen, it fades out the left and right bar item buttons. But with my custom button only text fades out and for back arrow button I have to do special handling. Is there a direct way of doing this:
Here is my code:
Adding custom button (PS: I have to go by custom button for some reasons):
- (void)showCustomBackButton {
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:#"Back" forState:UIControlStateNormal];
UIImage *backArrow = [UIImage imageNamed:#"backArrowImage"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:#"backArrow"];
[imageView setFrame:CGRectMake(kScreenOrigin, topMargin, backArrow.size.width, backArrow.size.height)];
[aCustomBackButton addSubview:imageView];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8.0;
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
aLeftBarButtonItem.tag = 100;
self.navigationItem.leftBarButtonItems = #[aNegativeSpacer, aLeftBarButtonItem];
}
- (void)showCustomBackButtonFaded {
UIBarButtonItem *aBarButtonItem = [self.navigationItem.leftBarButtonItems lastObject];
if (aBarButtonItem && aBarButtonItem.tag == 100) {
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:#"Back" forState:UIControlStateNormal];
UIImage *backArrow = [UIImage imageNamed:#"fadedBackArrow"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:backArrow];
[imageView setFrame:CGRectMake(kScreenOrigin, topMargin, backArrow.size.width, backArrow.size.height)];
[aCustomBackButton addSubview:imageView];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8;
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
self.navigationItem.leftBarButtonItems = #[aNegativeSpacer, aLeftBarButtonItem];
}
}

Putting 4 navigation bar buttons

i am new to iOS programming and i am wondering if it is possible to put 4 bar buttons into the UINavigationBar? I have tried a few methods that i have found on stack overflow but the buttons positions are not equal.
Here is a sample screenshot
.
The method that i used to code my navigation bar is:
UIToolBar *tools = [[UIToolBar alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
UIImage *backImage = [UIImage imagedName:#"backIcon.png"];
UIImage *shareImage = [UIImage imagedName:#"shareIcon.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
// create the button and assign the image to the leftsidebutton
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setImage:shareImage forState:UIControlStateNormal];
shareButton.frame = CGRectMake(0, 0, shareImage.size.width, shareImage.size.height);
//create space between the buttons
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *shareBarButton = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
self.navigationItem.hidesBackButton = YES;
NSArray *leftActionButtonItems = #[customBarButton, bi, shareBarButton];
[tools setItems:leftActionButtonItems animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
I did the same for rightBarButtonItem but it is not working so well.
Any help will be greatly appreciated! Thanks!
Try this one :
Edit :
it display Proper check it :
take .h file :
UIToolbar *toolBar_T;
UIBarButtonItem *item1,*item2,*item3,*item4;
take .m file :
toolBar_T=[[UIToolbar alloc] init];
toolBar_T.frame=CGRectMake(0,5,168, 44);
[self.view addSubview:toolBar_T];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(button0:) forControlEvents:UIControlEventTouchUpInside];
button0.frame = CGRectMake(0, 7, 35,29);
item1 = [[UIBarButtonItem alloc] initWithCustomView:button0];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(0, 7, 35,29));
item2 = [[UIBarButtonItem alloc] initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"3.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(button2:) forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(0, 7, 35,29);
item3 = [[UIBarButtonItem alloc] initWithCustomView:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setImage:[UIImage imageNamed:#"4.png"] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(button3:) forControlEvents:UIControlEventTouchUpInside];
button3.frame = CGRectMake(0, 7, 35,29);
item4 = [[UIBarButtonItem alloc] initWithCustomView:butto3];
NSArray *buttons = [NSArray arrayWithObjects: item1, item2,item3, nil];
[toolBar_T setItems: buttons animated:NO];
toolBar_T.barStyle=-1;// For Clear backgroud
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar_T];
Hope it will help.
Happy coding...
You can try smth like this...
-(void)setupToolbarButtons:(UIViewController *)navC
{
//create a toolbar in the right
UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 135, 44.01)];
tools.delegate = self;
tools.barTintColor = [UIColor clearColor];
//create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];
//create a standart 'play' button
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'stop' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'fast forward' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'pause' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:nil];
[buttons addObject:bi];
[bi release];
//stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
//and put the toolbar in the nav bar
navC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
}
...also you can add a spacer if it's needed
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
and if something is wrong in vertical spacing, then add this
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
UINavigationBar is Inherits from UIView, so you can simply add any object in.
Try the following code
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:customBarButton, bi, shareBarButton, nil];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:BUTTON OBJECTS, nil];

Resources