在我的应用程序中,我有多个视图,一些视图需要同时支持纵向和横向,而其他视图只需要支持纵向.因此,在项目总结中,我 Select 了所有方向.

以下代码用于在iOS 6之前的给定视图控制器上禁用横向模式:

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

由于iOS6中不推荐使用shouldAutorotateToInterfaceOrientation,我将上面的内容替换为:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMask.Portrait;
}

当视图出现时,这个方法被正确调用(我可以设置一个断点来确保这一点),但是界面仍然会旋转到横向模式,而不管我只返回纵向模式的遮罩.我做错了什么?

目前似乎不可能构建一个每个视图都有不同方向要求的应用程序.它似乎只遵循项目摘要中规定的方向.

推荐答案

如果您使用的是UINavigationController as the root window controller,则为its shouldAutorotate&;supportedInterfaceOrientations这个名字叫.

如果你用的是UITabBarController等等.

因此,要做的事情是对导航/选项卡栏控制器进行子类化,并覆盖其shouldAutorotate&;supportedInterfaceOrientations种方法.

Objective-c相关问答推荐

定义缓存变量时在objective-c中使用static关键字

UISearchDisplayController 没有结果tableView?

在 viewDidAppear 之前无法正确设置框架

调用了dismissViewControllerAnimated,但没有解除ViewController

仅替换 NSString 中子字符串的第一个实例

如何在使用 React Native 时实现 SSL 证书固定

如何知道 NSAssert 在发布版本中是否被禁用?

Objective-C 的文档生成器?

StoryBoard 助理编辑器停止显示相关文件

首次提交应用内购买以供审核

iPhone 键盘、完成按钮和 resignFirstResponder

获取包含在 NSString 中的文件的扩展名

MKAnnotation 图像偏移与自定义引脚图像

Objective C 中的调用方法或发送消息

IOS 中的圆形进度条

创建 NSTextField标签的示例代码?

如何为 iPhone 6/7 自定义边到边图像指定尺寸?

如何在键盘上添加完成按钮?

UITableView 在半透明的导航栏下

如何将按钮放在不会在 iOS 中随表格滚动的 UITableView 上