Remove back arrow in iOS7 - ios

I want to incorporate a custom back button - I'm able to get the above result using
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-btn"] style:UIBarButtonItemStylePlain target:nil action:nil];
but how do you remove the native blue button?

Use the below code to hide the back arrow:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-btn"]
style:UIBarButtonItemStylePlain
target:nil
action:nil];
if ([UINavigationBar instancesRespondToSelector:#selector(setBackIndicatorImage:)]) {
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage alloc] init]];
}

In iOS7+ you can use navigationBar's backIndicatorImage and backIndicatorTransitionMaskImage to achieve the custom arrow that you want. Swift code below:
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named:"button-backArrow18x15")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named:"button-backArrowMask18x15")
If you want to hide the "back" text you can set the title of the previous view to be an single space " " or use a custom UIBarButtonItem that doesn't have a title.

You can hide backbutton
self.navigationItem.hidesBackButton=YES;
To create custom leftbarbutton you can use this:
UIButton* someButton = [UIButton buttonWithType:UIButtonTypeCustom];
[someButton setFrame:frame];
[someButton setBackgroundImage:#"youArroImageName" forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* someBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setLeftBarButtonItem:someBarButtonItem];

Check this one:
UIImage *faceImage = [UIImage imageNamed:#"back_arrow.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[face addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationItem setHidesBackButton:YES animated:YES];
[self.navigationItem setBackBarButtonItem:nil];
-(IBAction)handleBack:(id)sender
{
// [self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}

Thanks to all the answerers - found a simpler way of doing it...
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-btn"] style:UIBarButtonItemStylePlain target:self action:#selector(backButtonAction:)];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
[self.navigationItem setHidesBackButton:YES animated:NO];
[self.navigationItem setBackBarButtonItem:nil];
-(void) backButtonAction:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

Swift 4 (if you just want to hide it)
let mask = UIImage()
navigationController?.navigationBar.backIndicatorImage = mask
navigationController?.navigationBar.backIndicatorTransitionMaskImage = mask

Why can't you try with the Custom UIButton :
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:#"back-btn"] forState:UIControlStateNormal];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.backBarButtonItem = backBtn;

I just faced the exact same problem, here's what I ended up doing:
// First set the back button text to an empty string
viewController.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
// Set the back-indicator image to the custom image (scaled to 20x20)
UIImage *img = [MyUtils imageWithImage:[UIImage imageNamed:#"back"] scaledToSize:CGSizeMake(20, 20)];
[[UINavigationBar appearance] setBackIndicatorImage:img];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:img];
// Set the tint color to WHITE
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
Here's the image-scaling method:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Credit for the scaling method above goes to Paul Lynch on this SO question: (The simplest way to resize an UIImage?)

Related

Why does my custom Navigation bar back button overlap/duplicated (Xcode 7.0)?

I added the following to customize my back button in my navigation bar but the image just overlaps/duplicated:
http://i.stack.imgur.com/eyzJC.png
- (void)viewWillAppear:(BOOL)animated
{
//take out the label following the button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
//set image for back button
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"backButtonImage.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"backButtonImage.png"]];
self.navigationItem.backBarButtonItem = backButton;
}
It's supposed to be an image of one black arrow facing to the left.
Before it worked fine but now it does this when I simulate it. Any insight and help would be appreciated.
Thank you
This should work for you.
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 50, 44);
back.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[back addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[back setImage:[UIImage imageNamed:#"backButtonImage.png"] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: back];
add a function:
-(void) back
{
[self.navigationController popViewControllerAnimated:YES];
}
Add this code
[UINavigationBar appearance].barTintColor = [UIColor colorWithWhite:1.00 alpha:1];
It should solve your problem.

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

iOS custom right navigation bar

I'm trying to set an image for a right bar item in my navigation controller but iOS 6 keeps showing a black glow. I have tried a number of solutions from stack overflow but can't get it to work. The current code I have is this:
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gear"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(someMethod)];
[rightItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
[[self navigationItem] setRightBarButtonItem:rightItem];
In iOS7 is looks like this which is what I want:
This is how it looks in iOS6
try this one :
UIImage *faceImage = [UIImage imageNamed:#"gear.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );//set bound as per you want
[face addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.rightBarButtonItem = backButton;
may it will help you.
Create a UIButton first with the image, then do:
[[UIBarbuttonItem alloc] initWithCustomView:button];
Use custom view:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; // change this to use your image
[rightButton addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightButtonItem;
Try this:
UIImage* image = [UIImage imageNamed:#"sample_Image.png"];
CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(MethodName:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn =[[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setRightBarButtonItem:barBtn];

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

Resources