iOS 7: How to set UIBarButtonItem backButtonBackgroundImage for UIControlStateHighlighted?
ios, ios7, navigationbar, uibarbuttonitem
Solution
Currently Apple has bug on `interactivePopGestureRecognizer` (which makes to freeze navigation controller's view after swiping back on push animation, you will see `nested pop animation can result in corrupted navigation bar` warning in console), by the way, we can make a small hack to work around that bug.
Here is a solution that works fine for me,
Subclass a NavigationController class and make it to delegate the gesture
@interface CBNavigationController : UINavigationController
@end
@implementation CBNavigationController
- (void)viewDidLoad
{
__weak CBNavigationController *weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate = weakSelf;
}
}
// Hijack the push method to disable the gesture
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = NO;
[super pushViewController:viewController animated:animated];
}
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
// Enable the gesture again once the new controller is shown
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = YES;
}
@end
When the user starts swiping backwards in the middle of a transition, the pop events stack up and "corrupt" the navigation stack. My workaround is to temporarily disable the gesture recognizer during push transitions, and enable it again when the new view controller loads. Again, this is easier with a UINavigationController subclass.
After this, you can calmly use `item.leftBarButtonItem` and `UIButton` as custom view.
Problem
I am trying to set the background image for back button in normal and highlighted states. ``` - (void)configureBackButtonInNavigationItem:(UINavigationItem *)item { UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:NULL]; [backBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateNormal]; [backBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateHighlighted]; // white arrow image UIImage *normalImage = [[[UIImage imageNamed:@"btn_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0.f, 17.f, 0.f, 0.f)]; // orange arrow image UIImage *pressedImage = [[[UIImage imageNamed:@"btn_on_press"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0.f, 17.f, 0.f, 0.f)]; [backBarButtonItem setBackButtonBackgroundImage:normalImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [backBarButtonItem setBackButtonBackgroundImage:pressedImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; [backBarButtonItem setBackgroundImage:normalImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [backBarButtonItem setBackgroundImage:pressedImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; NSLog(@"NORMAL: %@ HIGHLIGHTED: %@", [backBarButtonItem backButtonBackgroundImageForState:UIControlStateNormal barMetrics:UIBarMetricsDefault], [backBarButtonItem backButtonBackgroundImageForState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]); item.backBarButtonItem = backBarButtonItem; NSLog(@"NORMAL: %@ HIGHLIGHTED: %@", [backBarButtonItem backButtonBackgroundImageForState:UIControlStateNormal barMetrics:UIBarMetricsDefault], [backBarButtonItem backButtonBackgroundImageForState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]); } ``` The output is following: ``` NORMAL: <_UIResizableImage: 0x16b55e10> HIGHLIGHTED: <_UIResizableImage: 0x16b593d0> NORMAL: <_UIResizableImage: 0x16b55e10> HIGHLIGHTED: <_UIResizableImage: 0x16b593d0> ``` But observed result for highlighted state is just dimming of what was set to the normal state instead of using the correct highlighted image. Normal: Highlighted (Arrow is still white, button is dimmed unexpectedly): Please do not post answers regarding usage of `leftBarButtonItem` or `UIButton` as custom view. Both these approaches break swipe-to-go-back behaviour available on iOS 7. UPD: filled radar #17481106 regarding this issue. UPD2: radar #17481106 fixed in iOS 8.