阻止 UIView 旋转到横向

问题描述:

我在导航控制器中有一个 UIView,我试图阻止这个视图进入横向,但是我尝试使用的方法从不触发......代码低于任何帮助将不胜感激..

I have a UIView thats inside a navigation controller, I am trying to prevent this view from going into Landscape however the method I am trying to use never fires.. code is below any help would be greatly appreciated..

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

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

You should set return NO; for the parent navigation controller or on a UIViewController, not a UIView.

此外,这与更少的代码一样有效:

Also, this works just the same with less code:

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

iOS 6

从 iOS 6 开始,shouldAutorotateToInterfaceOrientation: 已被弃用.如果视图控制器没有覆盖 supportedInterfaceOrientations 方法,UIKit 从应用程序委托或应用程序的 Info.plist 文件中获取默认旋转.

iOS 6

As of iOS 6, shouldAutorotateToInterfaceOrientation: is deprecated. If the view controller does not override the supportedInterfaceOrientations method, UIKit obtains the default rotations from the app delegate or the app’s Info.plist file.

您将需要使用:

- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);