仅适用于iPhone或iPad的横屏模式

37

我想创建一个不使用竖屏模式的应用程序。

我不确定是否需要编辑plist文件或者除了plist文件还需要编写代码。

4个回答

47

此处找到代码

以横向模式启动

iPhone OS中的应用程序通常会以竖屏模式启动,以匹配主屏幕的方向。如果您的应用程序在竖屏和横屏模式下运行,则应始终以竖屏模式启动,然后让其视图控制器根据设备的方向旋转界面。但是,如果您的应用程序仅在横屏模式下运行,则必须执行以下步骤才能使其最初以横向方向启动。

  • 在应用程序的Info.plist文件中,添加UIInterfaceOrientation键并将其值设置为横向模式。对于横向方向,可以将此键的值设置为UIInterfaceOrientationLandscapeLeft或UIInterfaceOrientationLandscapeRight。

  • 以横向模式布局您的视图,并确保它们的自动调整选项设置正确。

  • 覆盖您的视图控制器的shouldAutorotateToInterfaceOrientation:方法,并仅对期望的横向方向返回YES,对竖向方向返回NO。


总是忘记 shouldAutorotateToInterfaceOrientation 这一步骤,如果没有它,你的整个用户界面会侧着出现... - Kendall Helmstetter Gelner

29
要使您的应用程序仅限于横向模式,您应该使用“支持的界面方向”。 (目标 -> 您的应用程序 -> 支持的界面方向 -> 左右横向)

Supported Interface Orientations

您还应该在应用的 Info.plist 文件中设置应用的方向(Info.plist file),通过将 Supported interface orientations 键附加到值为 Landscape (left home button)Landscape (right home button) 的值。您可以使用 willRotateToInterfaceOrientation 和/或 didRotateFromInterfaceOrientation 来处理方向更改。

shouldAutorotateToInterfaceOrientationiOS 6及以上版本中已被弃用。

返回UIDeviceOrientationLandscapeLeft/Right以使您的应用程序“横向”:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

同时,您也可以更改应用程序的 Info.plist视图方向(如上所述)。


此外,我建议在 属性检查器 中将您的视图方向更改为 Landscapelandscape

1
这种选择以前不存在,但了解新的选项是很好的。 - Cocoa Dev

26

你也可以将其简化为

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

10

编辑plist文件以仅支持横向屏幕,然后确保在每个uiviewcontroller / uitabbar等中,shouldAutoRotateToInterfaceOrientation 方法中的return语句为 return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));


3
甚至更好的是:if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) { return YES; } return NO;如果(界面方向为横向) { 返回是; } 返回否; - Sangraal
5
甚至更简短:返回(UIInterfaceOrientationIsLandscape(interfaceOrientation)) - Sandro Meier
更短,无需括号 :-) - Daniel Bauke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接