5

I have created custom UINavigation Back Button. But the origin of the button is different in iOS 6 and iOS 7.

iOS 6 look:

enter image description here

iOS 7 look:

enter image description here

How to set UINavigation Back Button origin in iOS 7 to be the same like in iOS 6?

3 1
  • 15
  • 4
Serghei Pogor
  • 108
  • 1
  • 10
  • 2
    Others had similar issues, maybe these links can help you [1]: http://stackoverflow.com/questions/18861201/uibarbuttonitem-with-custom-view-not-properly-aligned-on-ios-7-when-used-as-left [2]: http://stackoverflow.com/questions/19233591/custom-uibarbuttonitem-alignment-off-with-ios7/ – cania Mar 20 '14 at 09:15
  • How do you created `Back Button`? – Akhilrajtr Mar 20 '14 at 09:34

2 Answers2

5

Use this code to fix left bar button position:

    //First add the following macro:
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

    //Then customize your navigation bar:
    - (void) initNavigationBar
    {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        {
            negativeSpacer.width = -10;
        }
        else
        {
            negativeSpacer.width = 0;
        }

        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:_customBackButton];
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,backButton];
    }
iOS Dev
  • 4,143
  • 5
  • 30
  • 58
0

Try this:

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

- (void)setupNavigationButton {
     UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height)];
    //Align button left with view
    if (SYSTEM_VERSION_LESS_THAN(@"7")) {
        backButtonView.bounds = CGRectOffset(backButtonView.bounds, -8, -5);
    } else {
        backButtonView.bounds = CGRectOffset(backButtonView.bounds, 2.5, 0);
    }
    [backButtonView addSubview:button];

    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
    self.navigationController.navigationItem.leftBarButtonItem = customBarItem;
}
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25