2

When I push a viewcontroller on my navigation stack, the back button appears to be against the left side of the screen. How can I get the regular padding of the back button?

No padding

Here is how I present the view controller:

- (void)goToCollection:(UIButton *)btn {
    Card *colCard = (Card *)btn.userData;
    WViewController *vc = [WViewController new];
    NSString *colID = [[colCard.href componentsSeparatedByString:@"/"] lastObject];
    vc.collectionID = colID;
    [self.navigationController pushViewController:vc animated:YES];
}

Here's my view setup in didFinishLaunchingWithOptions :

//Profile View
ProfileViewController *pv = [ProfileViewController new];
UINavigationController *profNav = [[UINavigationController alloc] initWithRootViewController:pv];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:profNav];
[self.window setTintColor:[UIColor Primary]];
[self.window makeKeyAndVisible];
Halpo
  • 2,982
  • 3
  • 25
  • 54
  • Is the image you posted an example of your app or what you expect to see? This Back button with chevron seems consistent with other apps to me. Otherwise, can you post your code so we can see how you are presenting the new view controller? – William Smith Sep 09 '15 at 15:07
  • it's quite hard to see but the chevron is touching the left boundary of the screen – Halpo Sep 09 '15 at 15:14
  • I also get 'Unbalanced calls to begin/end appearance transitions for' when navigating – Halpo Sep 09 '15 at 15:17
  • Have you ever checked with the view debugger?https://developer.apple.com/library/ios/recipes/xcode_help-debugger/using_view_debugger/using_view_debugger.html – Jason Nam Sep 09 '15 at 15:24
  • The UINavigationController in question is the root controller of the app delegate's 'window' - when I embed this controller in a UITabBarController there is no longer an issue – Halpo Sep 09 '15 at 15:37

2 Answers2

4

As per apple documentation, you can not edit or modify back button in any case. Only allowed operation for back button is show and hide . So if you want something to do with back button, you need to hide it and create a customise left bar button item as back button.

self.navigationItem.hidesBackButton = YES; //hides back button
    UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; // creates a new button
    [myButton setImage:[UIImage backButtonImage]; // sets image for new button
    [myButton setContentEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];//content edgeInset to provide required padding
    [myButton addTarget:self action:@selector(backToHOmePage) forControlEvents:UIControlEventTouchUpInside];//adding target for new button
    UIBarButtonItem *customBackBtn = [[UIBarButtonItem alloc] initWithCustomView:myButton]; //custom bar button item
    self.navigationItem.leftBarButtonItem = customBackBtn; //assigning to left bar button item

-(void)backToHOmePage
{
    [self.navigationController popViewControllerAnimated:YES];
} // method to be triggered on tapping above button
Vikas Mishra
  • 246
  • 1
  • 12
  • +1 for `setContentEdgeInsets`. I struggled to add padding around a UIButton's text. First, I tried `myButton.titleEdgeInsets = UIEdgeInsetsMake(5,5,5,5)` with positive values--but that shrunk the padding. Negative values did not increase the padding. To increase the padding around the text in Swift 2: `myButon.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5)` – 7stud Jan 09 '16 at 12:54
1

Put this in your AppDelegate

[[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault];
HannahCarney
  • 3,441
  • 2
  • 26
  • 32
  • I don't really want to hack this - I think there is an issue with my Navigation Stack – Halpo Sep 09 '15 at 15:49
  • is the frame of your UINavigationController the same width as the UIViewController? Show us the code for when you created the navigation controller. – HannahCarney Sep 09 '15 at 15:57
  • Instead of self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; and [self.window setRootViewController:profNav]; why don't you try self.window.rootViewController = profNav; – HannahCarney Sep 09 '15 at 16:08
  • I am not using storyboards / nib so I need to create the UIWindow myself – Halpo Sep 09 '15 at 16:14
  • Have you seen this post? http://stackoverflow.com/questions/5761183/change-position-of-uibarbuttonitem-in-uinavigationbar – HannahCarney Sep 09 '15 at 16:37