Setting a Custom Image for a Back Button on UINavigationBar
try this code
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
then define goback method like this
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
Since iOS 7.0 there is a new method in API backIndicatorImage
. You can use it instead of setBackButtonBackgroundImage
if you don't want the image to be stretched to fit the text (for example if you want a fixed size custom back arrow). Here's an example in swift:
let image = UIImage(named: "back_button")
UINavigationBar.appearance().backIndicatorImage = image
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image
You can hide the text from the button using this trick:
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -66), for: .default)
for Swift 5
var backButtonImage = UIImage(named: "back_button")
backButtonImage = backButtonImage?.stretchableImage(withLeftCapWidth: 15, topCapHeight: 30) UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)