Search code examples
ios6uibarbuttonitemback-buttonios5.1uiappearance

setBackButtonBackgroundImage Weard Behaviour on iOS5.1


I have two différent behaviour on ios5.1 and ios6.0 with

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:offBackButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

On iOS 6.0 everything is OK On iOS 6.0

On iOS5.1 ??? On iOS 5.1 ???

Is there something I'm not doing well ?


Solution

  • You need to specify a stretchable UIImage with UIEdgeInsets as below:

    UIImage *normalStateImage = [[UIImage imageNamed:@"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];
    UIImage *pressedStateImage = [[UIImage imageNamed:@"image-pressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];
    
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:normalStateImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:pressedStateImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    

    iOS 6.x is clever enough to determine the image sizes and stretch according to it.