繁体   English   中英

在iOS7和iOS8中自动旋转

[英]Auto rotate in iOS7 and iOS8

我正在开发一个纵向模式的应用程序。 但是我希望一个视图控制器既可以横向显示也可以纵向显示。

我尝试了以下代码,但它不起作用(未调用)

- (BOOL) shouldAutorotate
{
    return NO;
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationMaskPortrait;
}

首先,您需要在plist中设置应用程序支持的所有方向,这可以在项目的“常规”标签中的“部署信息”下进行,例如: 在此处输入图片说明

然后,您可以使用supportedInterfaceOrientations方法,

我假设您以模态呈现视图控制器,因此只需在呈现的viewController上重写它即可,该视图控制器仅需纵向使用:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

并在显示的viewController中也应支持横向,请使用:(或您想要的任何方向蒙版)

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

PS-模态显示的viewController和推入navigationController堆栈的viewController有不同的行为:

  • modalViewController将调用其自己的supportedInterfaceOrientations ,并将支持这些方向
  • pushedViewController将调用其navigationController supportedInterfaceOrientations ,并支持这些方向。

因此,如果以模态形式显示viewController,则需要重写其自己的supportedInterfaceOrientations ,但是如果按下此viewController,则需要在navigationController中设置一些BOOL属性,以便它将知道要支持的方向。 我建议您以模态形式显示此viewController,对于不同的设备方向使用modalViewController更自然。

PS#2 :关于shouldAutorotate :如果返回“ NO”,则不调用supportedInterfaceOrientations ,因此返回“ YES”。 它只说设备旋转时是否自动旋转。 如果返回“ NO”,则需要显式旋转viewController。

好吧,我希望我能帮助并且没有写出一个完全没有考虑到你所问的答案的答案... :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM