Neigaard
Neigaard

Reputation: 4050

UINavigationBar back button without text and fixed size

I have a custom png I set as the back button for the UINavigationBar like this:

UIImage *navBackgroundPortait = [[UIImage imageNamed:@"nav_bg_portrait.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundPortait forBarMetrics:UIBarMetricsDefault];

In my view controller in the viewDidLoad method I use this hack to display the back button with no title/text:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:nil action:nil];

But my back button png gets stretched in width, and I really would like it to not do that at all. I simply want to display a static png image as my back button on all view's with no text on it, and never ever have it stretched. Can I do this, and how do I do it?

Best regards
Søren

Upvotes: 2

Views: 5592

Answers (2)

edzio27
edzio27

Reputation: 4140

Use this:

UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
[backButton setImage:[UIImage imageNamed:@"nav_bg_portrait.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(popNavigationControllerFunction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Upvotes: 6

Alphapico
Alphapico

Reputation: 3043

Because you tell image to get stretched with resizableImageWithCapInsets: If you want to display image as it is, just do:

UIImage *navBackgroundPortait = [UIImage imageNamed:@"nav_bg_portrait.png"];

Upvotes: 0

Related Questions