Custom UINavigationBar backButton?

I am making an iPhone application that allows the user to return to the UINavigationBar. However, what he looks like now is terrible. I am trying to customize it using my own image (minus the default UIBarButtonItem background). My image includes my custom background, but you can still see part of the button on the left and right.

UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_button_normal.png"] style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)] autorelease]; self.navigationItem.leftBarButtonItem = cancelButton; 

Is there any way to remove this space? Is it possible to use UIButton to fully configure it? I did something in the interface builder where I dragged the UIButton to the right byte of the UINavigationBar and it works fine. Anyway, can I do this programmatically?

Thanks!

Here's what it looks like:

UINavigationBar

EDIT # 1:

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchDown]; [button setTitle:@"Show View" forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageNamed:@"back_button_normal.png"] forState:UIControlStateNormal]; UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; [self.navigationItem setLeftBarButtonItem:barButtonItem]; 

This is how I make my background for the UINavigationBar (places in my RootViewController):

 @implementation UINavigationBar (UINavigationBarCustomDraw) - (void) drawRect:(CGRect)rect { [self setTintColor:[UIColor colorWithRed:0.85f green: 0.85f blue:0.85f alpha:1]]; if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@"Back to ..."]) { [[UIImage imageNamed:@"UINavigationBar_background.png"] drawInRect:rect]; CGRect frame = CGRectMake(0, 0, 320, 44); UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; [label setBackgroundColor:[UIColor clearColor]]; label.font = [UIFont boldSystemFontOfSize: 20.0]; label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1]; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.text = self.topItem.title; self.topItem.titleView = label; } else { [[UIImage imageNamed:@"login_button.png"] drawInRect:rect]; self.topItem.titleView = [[[UIView alloc] init] autorelease]; } } @end 
+6
source share
3 answers

If you want to add UIButton to the navigation bar via obj-c, your code should look like this:

 UIButton *button = /* initialize your button */ UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; [self.navigationItem setLeftBarButtonItem:barButtonItem]; 
+11
source

Combined two answers and added Back Button functionality

 // Custom Navigation Bar Back Button // Create Button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0,0,54,32); // width=54, height=32 // Set Button Image NSString *backButtonURL = [[NSBundle mainBundle]pathForResource:@"back" ofType:@"png"]; UIImage *backButtonImage = [UIImage imageWithContentsOfFile:backButtonURL]; [button setBackgroundImage:backButtonImage forState:UIControlStateNormal]; // Important: Set Button Action to go back [button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside]; 
+14
source

The problem is that you are using UIButtonTypeRoundedRect instead of UIButtonTypeCustom . You can also use:

 UIImage *image = [UIImage imageNamed:@"back_button_normal.png"] button.frame = CGRectMake(0, 0, image.size.width, image.size.height); 
+2
source

Source: https://habr.com/ru/post/888117/


All Articles