use*_*652 30 iphone xcode objective-c orientation ios
我正在制作一个iPhone应用程序,我需要它处于纵向模式,所以如果用户侧向移动设备,它不会自动旋转.我怎样才能做到这一点?
the*_*ner 51
要禁用特定View Controller的方向,您现在应该覆盖supportedInterfaceOrientations和preferredInterfaceOrientationForPresentation.
- (NSUInteger) supportedInterfaceOrientations {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskPortrait;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
// Return the orientation you'd prefer - this is what it launches to. The
// user can still rotate. You don't have to implement this method, in which
// case it launches in the current orientation
return UIInterfaceOrientationPortrait;
}
Run Code Online (Sandbox Code Playgroud)
如果您的目标是iOS 6之前的版本,则需要使用该shouldAutorotateToInterfaceOrientation:方法.通过更改何时返回yes,您将确定它是否将旋转到所述方向.这只允许正常的纵向方向.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
// Use this to allow upside down as well
//return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Run Code Online (Sandbox Code Playgroud)
请注意,shouldAutorotateToInterfaceOrientation:iOS 6.0中已弃用.
sky*_*der 38

Tho*_*eek 28
对于那些错过它的人:您可以使用项目设置屏幕来修复整个应用程序的方向(无需覆盖每个控制器中的方法):

它就像切换支持的接口方向一样简单.您可以通过单击左侧面板中的项目>应用程序目标>摘要选项卡找到.
如果您想禁用iPhone 和 iPad的横向方向。
转至“目标”并转至“常规”选项卡。请参阅下面的屏幕并取消选择横向左和横向右。
在这种情况下,只有 iPhone 横向模式将被禁用,而 iPad 则不会。对于 iPad,所有模式均已启用。如果您想选择从通用到 iPad 的设备选项。它看起来像这样。请参见下面的屏幕。
现在您需要取消选择除 iPad肖像之外的所有模式。请参阅下面的屏幕截图。
现在,您已成功禁用所有设备上除纵向之外的所有模式。
适用于 iPhone 和 iPad(通用)的最简单的解决方案 - 它删除了info.plist文件或Project -> Info -> Custom iOS Target Properties中不必要的方向。
只需从列表中添加或删除方向项:
如果您创建了一个新的 Xcode 13.3 项目并在“项目”>“常规”>“部署”中取消选中不必要的方向复选标记,则没有帮助。检查 Target > Build Settings - 有 2 行覆盖全局设置。
| 归档时间: |
|
| 查看次数: |
50190 次 |
| 最近记录: |