UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"back-button-item"];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:13.f];
backButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
[backButton setContentEdgeInsets:UIEdgeInsetsMake(0, 7, 0, 0)];
UIBarButtonItem *backButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
[self.navigationItem setLeftBarButtonItem:backButtonItem animated:NO];
}
I normally create a custom subclass of UIViewController and then inherit any other controller that wants to use this back button from this controller(and any other NavigationBar skinning hacks). There are cleaner approaches using method swizzling but this way works for me, and it's fairly simple to understand.
So for instance, the inheritance chain looks like this:
UIViewController -> BILBaseViewController -> BILHomeViewController
BILHomeViewController is added to a UINavigationController. Other view controllers also inherit from BILBaseViewController and then the back button is displayed.
The back methods looks like this BTW:
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Hope this helps, give me a shout if not.
Cameron