【问题标题】:How to lock orientation in UINavigationControllers如何在 UINavigationControllers 中锁定方向
【发布时间】:2012-09-26 11:33:09
【问题描述】:
由于ShouldAutorotateToInterfaceOrientation 在iOS 6 中已弃用,我无法在我的应用程序中锁定方向。在我的应用程序中,我有多个视图的UINavigationControllers,一些视图需要同时支持纵向和横向,而其他视图只需要支持纵向。我该如何解决这个问题,请给我一些建议。
谢谢
【问题讨论】:
标签:
iphone
uinavigationcontroller
orientation
ios6
【解决方案1】:
此功能仅适用于iOS 6
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
【解决方案2】:
您可以通过继承 UINavigationController 来锁定方向。
Here is an excellent link on how to do that.
然后在子类 UIViewController 中重写以下方法来实现锁定(在本例中为纵向锁定)。
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
【解决方案3】:
在 viewDidLoad 中添加以下代码
UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
用于锁定纵向方向
添加功能
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( UIInterfaceOrientationIsPortrait(interfaceOrientation));
}
用于锁定横向
添加功能
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( UIInterfaceOrientationIsLandscape(interfaceOrientation) );
}