UINavigationBar back button without text and fixed size

ios, uinavigationbar, uinavigationcontroller, uinavigationitem

Solution

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

Problem

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

Original source