Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 custom back button

I want to use custom back button. in iOS 6 everything is perfect but iOS 7 is strange.

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[[UIImage imageNamed:@"back_button_normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12.0, 0, 12.0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

first, it has no iOS 7 arrow and no background image.

(Russian locale)

initial state

then, if you press the button background image appears. Also i had background image set for UIControlStateHighlighted state and when you hold the button pressed highlighted image appears too. After any back button once pressed all back buttons have background image.

once pressed

BUT! If you present modal view controller, dismiss it, then push any view controller - iOS 7 arrow will appear at every back button.

I use DP5. Is that a UIKit bug?

PS Also i tried to create back button manually, using UIBarButtonItem, set background image to it, then self.navigationItem.backBarButtonItem = barButtonItem; Did not help. Then i tried to set background image to disabled state and change enabled property of my bar button item, did not help too.

enter image description here

like image 914
storoj Avatar asked Sep 16 '13 09:09

storoj


People also ask

How to get the back button on iPhone?

Turn on Back Tap Check that you have the latest version of iOS on your iPhone 8 or later. Go to Settings > Accessibility > Touch, and tap Back Tap. Tap Double Tap or Triple Tap and choose an action. Double or triple tap on the back of your iPhone to trigger the action you set.

How do I hide the back button?

Way 2: Touch “Settings” -> “Display” -> “Navigation bar” -> “Full screen gestures”. The navigation bar will be hidden and you will be shown “Gesture demos” to explain how to operate the “Go back”, “Go to Home screen” and “Open Recents” functions.


2 Answers

This is not a bug, this how Back button looks in iOS 7. For example:

enter image description here

You should probably use the new concept for your application, and not to set background image for back button in iOS 7.

If you still want you back button have the same as it looked in iOS6 than you should probably create those back buttons manually:

- (void)loadView
{
    [super loadView];

    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 30.0f)];
    UIImage *backImage = [[UIImage imageNamed:@"back_button_normal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12.0f, 0, 12.0f)];
    [backButton setBackgroundImage:backImage  forState:UIControlStateNormal];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backButtonItem;
}

-(void) popBack {
  [self.navigationController popViewControllerAnimated:YES];
}

Edit: Not to break Swipe Gesture (Here is a source)

self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
like image 74
B.S. Avatar answered Nov 12 '22 00:11

B.S.


The custom background image not appearing on the first push was fixed in iOS 7 GM.

To hide standard back indicator use this code:

if ([UINavigationBar instancesRespondToSelector:@selector(setBackIndicatorImage:)]) { // iOS 7
    [navigationBarAppearance setBackIndicatorImage:[UIImage imageNamed:@"transparent_1px"]];
    [navigationBarAppearance setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"transparent_1px"]];
}
like image 22
kolyuchiy Avatar answered Nov 12 '22 02:11

kolyuchiy