你可以在iOS应用程序中全局禁用旋转吗?
我有一个由许多视图控制器组成的应用程序…在项目摘要中,我已经将纵向方向设置为唯一受支持的设备方向。
然而,应用程序仍然横向转向时会变得混乱。
我的问题是,有没有办法通过应用程序委托或什么东西全局禁用autorotation?
或者我必须进入我所有的视图控制器,并添加“shouldAutorotateToInterfaceOrientation”方法?
只是不想错过添加到一个或东西…
谢谢!
在根视图控制器的方法中:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); }
设置“返回NO”;
这应该为所有的意见。
在Info.plist中,展开“支持的界面方向”并删除横向项目,使您的应用程序只能在纵向模式下运行。
在努力设置UIViewController的shouldAutorotate
和supportedInterfaceOrientation
方法后,在iOS6中没有成功,我发现最有效的方法是在应用程序委托中进行设置。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }
但是返回UIInterfaceOrientationMaskPortraitUpsideDown
崩溃了我的应用程序。 我不知道我在做什么错误!
现在在info.plist
有三种设备方向键。
- 支持的界面方向(iPad)
- 支持的界面方向(iPhone)
- 支持的接口方向
第三个是我认为非通用的应用程序,其余两个是iPad和iPhone。
你应该给他们一个尝试。
从IOS 6开始,Haris Hussain的回答似乎已经被弃用,但是有一些新的方法可以限制/启用轮换。
以下是UIViewController头中列出的方法:
// New Autorotation support. - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0); - (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0); // Returns interface orientation masks. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);
请注意,如果您在已经旋转的状态下启动应用,shouldAutoRotate似乎不起作用!
如果您支持iPad,那么您不应该取消选中风景方向,因为这会阻止您的应用程序被App Store上的Apple接受。
为了防止在应用程序显示您的第一个屏幕之前旋转,将其放在AppDelegate.m中
这个方法在上面的iOS 7.1中工作和testing。
// G - fix for ipad. - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }