Custom Back Button for Navigation Bar - ios

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];
}

Related

Positioning image of navigationBar backButton

I add an image to my navigation controller back button like this:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"ITButton.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
It works fine, therefore, my image is not aligned with the < back symbol. The image is a little above the symbol, I want both centered. I don't know why. I've tried to adjust this using:
[self.navigationItem.backBarButtonItem setImageInsets:UIEdgeInsetsMake(10, 0, 0, 0)];
but nothing changed.
there are many ways to fix the issue, I was facing the same issue then I have Solved it this way
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if([self.navigationController.viewControllers objectAtIndex:0] != self)
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"home.png"] forState:UIControlStateNormal];
[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];
}

Hide Back text in navigation item

I need to set a back arrow by replacing back Text(button)in navigation bar. I can view the back arrow but i don't know how to hide the back Text in navigation item. Here's is my code:
with this am getting warning : : CGImageCreateWithImageProvider: invalid image size: 0 x 0.
(void)viewWillAppear:(BOOL)animated
{
UIImage *backButtonHomeImage = [[UIImage imageNamed:#"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem = nil;
}
The answer
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if([self.navigationController.viewControllers objectAtIndex:0] != self)
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"back-arrow.png"] forState:UIControlStateNormal];
[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];
}
may it will help.
Try this :
UIImage *backButtonHomeImage = [[UIImage imageNamed:#"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];
Create custom back button and add that button to Navigation controller's back button.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,54,32);
UIImage *backButtonImage = [UIImage imageNamed:#"back-arrow.png"];
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[button addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
Just set blank text to title of navigation bar before pushing to that view Controller as below code.
self.navigationItem.title=#" ";
I hope this would work
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

UIBarButtonItem background image and setting action

I created button in navigation bar with this code:
UIBarButtonItem *myButtonEn = [[UIBarButtonItem alloc] initWithTitle:#"EN" style:UIBarButtonItemStyleDone target:self action:#selector(buttonEN:)];
My action for this button:
- (void) buttonEN:(id)sender {
NSLog(#"English language");
}
This works fine but I have to change background and button's style (code below).
UIImage *imageEN = [UIImage imageNamed:#"cys_lang_en.png"];
UIButton *buttonImgEN = [UIButton buttonWithType:UIButtonTypeCustom];
buttonImgEN.bounds = CGRectMake( 0, 0, imageEN.size.width, imageEN.size.height );
[buttonImgEN setImage:imageEN forState:UIControlStateNormal];
UIBarButtonItem *myButtonEn = [[UIBarButtonItem alloc] initWithCustomView:buttonImgEN];
This is axactly what I need, but I need to set action
[myButtonEn setTarget:self];
[myButtonEn setAction:#selector(buttonEN:)];
This code is compiled with no errors but button does not respond.
Does anyone know what is wrong?
try this..
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 40.0f)];
[btn addTarget:self action:#selector(buttonEN:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"cys_lang_en.png"] forState:UIControlStateNormal];
UIBarButtonItem *eng_btn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = eng_btn;
}
-(void)buttonEN:(id)sender
{
NSLog(#"English language");
}
You are Missing action and target for button
You can set using,
[myButtonEn setTarget:self];
[myButtonEn setAction:#selector(buttonEN:)];
You might want to change apperance of this button.
[[UIBarButtonItem appearance] setBackgroundImage:<doneBackgroundImage>
forState:UIControlStateNormal
style:UIBarButtonItemStyleDone
barMetrics:UIBarMetricsDefault];
You don't need to use custom view for image,
Try to use:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStylePlain target:target action:#selector(selector)];

How to set UIBarButtonItem with out border

I have created view with barbutton as follows
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settingsicon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(settingsButtonClicked:)];
It is comming like this
but I want it as
how can I remove the border of the button?
Use this :
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:customButton];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:btton];
Hope it helps you.
Another way is to remove button's background image:
// portrait
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// landscape
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];

Can't customize pushViewController's back button

I've got a ViewController which opens up another ViewController by
[self.navigationController pushViewController:detailViewController animated:YES];
Now the new VC has got a back button in the navBar but I can't manage to give it the same background image as the navBar buttons from the first ViewController. I've tried with:
UIImage *buttonImage = [UIImage imageNamed: #"Button.png"];
[self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.backBarButtonItem setTitle:#"doesnt work either"];
But it doesn't work. What am I doing wrong? Thank you!
Try code snippet give below, it works fine for me:
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(pop)];
self.navigationItem.backBarButtonItem = barBtnItem;
Or you can do this as well:
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[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;
Enjoy Coding :)
Try this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(pushNav:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
-(void)pushNav {
[self.navigationController popViewControllerAnimated:YES];
}

Resources