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

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.

Related

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

Title of UINavigationBar becomes blank when segueing back to root VC

I've got a UINavigationController that segues to 2 other VC's. Whenever it reaches a certain VC with the current setup :
// set text
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.title = #"Title";
// make the nav bar transaparent
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setOpaque:NO];
// set white status bar
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
[self setNeedsStatusBarAppearanceUpdate];
//self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(16, 31, 22, 20)];
UIImage *backImage = [UIImage imageNamed:#"backButtonDetail"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
And I pop the VC, the title of the root vc becomes empty. However, whenever I'm segueing to a VC without this setup, but a more 'basic' setup, the title stays where it should.
Easiest thing I can recommend: use [UINavigationBar appearance] to set many of these customizations from the AppDelegate
Checkout out AppCoda's post on the subject. I've just run through it myself and it's pretty great

Remove back arrow in iOS7

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

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

Resources