问题
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:
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?
回答1:
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.
回答2:
As @Stonz2 mentioned, this appears to be a characteristic of presenting a modal from a detached view controller. I was having the same problem and remedied it by re-organizing my app so that it wasn't presenting from a detached controller.
You'll know if you're presenting from a detached controller if you get the following error message:
Presenting view controllers on detached view controllers is discouraged
回答3:
I was presenting from a detached viewController. I never got the warning. But I tried embedding that detached viewController in a NavigationController, and the problem went away.
来源:https://stackoverflow.com/questions/25415711/uibarbuttonitems-shift-position-when-uinavigationcontroller-is-presented-modally