Search code examples
iosobjective-cuistoryboarduibarbuttonitemuiappearance

UIBarButtonItem backButtonBackgroundImage: gets stretched


I am using UIAppearance to set a custom backButtonBackgroundImage as well as hide the back button title:

// Back Button Image
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"ZSSBackArrow"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];

This works really well, but the problem is that my arrow image get really stretched horizontally:

enter image description here

It should look like this:

enter image description here

Update: Using cap inset makes the image look like this:

enter image description here

Is there a way to prevent the image from being stretched?


Solution

  • Change the image's cap insets to make the image fit the way you want:

    UIImage *barButtonImage = [[UIImage imageNamed:@"ZSSBackArrow"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,width,0,0)];
    

    This will keep the width of the image as your specified width.

    You may need to play with the numbers a bit to get it just right.