Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButtonItems shift position when UINavigationController is presented modally

I'm presenting a UINavigationController modally, from within an iOS app extension:

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];       
nav.modalPresentationStyle = UIModalPresentationFormSheet;
nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:nav animated:YES completion:nil];

When the navigation controller appears, its root view controller's UIBarButtonItems jump position:

Jumping buttons

I'm creating and adding the buttons in viewDidLoad. They are just standard bar button items:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];

I'm not overriding viewDidAppear (which appears to be the point where the buttons jump).

Presenting this same navigation controller/root view controller from within my app, instead of the app extension, doesn't give me this same problem. Any ideas?

like image 218
blork Avatar asked Aug 20 '14 22:08

blork


1 Answers

I'm hoping someone else finds a better way to do this, but my current solution is to create stand alone buttons and then use [[UIBarButtonItem alloc] initWithCustomView:] with the button. Not ideal, and the indention is still there, but the weird jump doesn't happen anymore.

If the indention bothers you, you could also set negative fixed spaces on the outsides of the buttons to shift them closer to the edges.

Here's an example including the spacer:

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
<other button config here, set targets, whatever>
[button sizeToFit];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:-8.0f];

self.navigationItem.leftBarButtonItems = @[fixedSpace, leftBarButton];

This only happens to me on my share extension, and I don't have any problems related to presenting from a detached controller or anything like that.

like image 120
Stakenborg Avatar answered Oct 26 '22 15:10

Stakenborg