如果您使用的是 VC 之间呈现的模型,@David 的答案是正确的。
但是如果你使用UINavigationControllerpush/pop,事情会更复杂。
- (NSUInteger)supportedInterfaceOrientations 和 - (BOOL)shouldAutorotate 仅在设备旋转或根控制器更改后调用。 push/pop 不会成功。
在 iOS5 及之前的版本中,有一个 API:
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationPortrait];
但它已被弃用。 Apple 将其设为私有。
在iOS 6+中仍然可以使用objc runtime api调用,如:
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), @(UIInterfaceOrientationPortrait));
或
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait)
forKey:@"orientation"];
注意:它是私有 API,这可能会被 App Store 拒绝。
另一种棘手的方法是,只需呈现一个空 VC,然后在 viewDidAppear 中将其关闭,类似于:
[self presentViewController:[UIViewController new]
animated:NO
completion:^{
[self dismissViewControllerAnimated:NO completion:nil];
}];
这将使- (NSUInteger)supportedInterfaceOrientations 和- (BOOL)shouldAutorotate 被调用。
如果您对以上内容不满意。尝试制作自己的 NavigationController,或者
看看这个问题:Why can't I force landscape orientation when use UINavigationController?。