阻止UIView旋转到风景

时间:2012-07-11 21:27:36

标签: ios uiview uiinterfaceorientation

我在导航控制器中有一个UIView,我试图阻止这个视图进入Landscape但是我试图使用的方法永远不会触发..代码低于任何帮助将非常感激..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        return NO;
    }
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        return NO;
    }
    return NO;
}

3 个答案:

答案 0 :(得分:4)

您应该为父导航控制器设置return NO;,或者设置UIViewController,而不是UIView

此外,这与使用较少代码的情况相同:

iOS 5.x

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

iOS 6

自iOS 6起,shouldAutorotateToInterfaceOrientation:已弃用。如果视图控制器未覆盖supportedInterfaceOrientations方法,UIKit将从应用委托或应用的Info.plist文件中获取默认轮播。

您需要使用:

- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);

答案 1 :(得分:1)

另一个选择是修改部署信息。

Screenshot

答案 2 :(得分:0)

此方法需要在UIViewController实例中实施,而不是UIView

另外,您实际上是为所有方向返回NO,这是不正确的。您需要至少返回一个方向YES(最常见的是UIInterfaceOrientationPortrait)。