iOS 6轮换:supportedInterfaceOrientations不起作用?

stk*_*stk 36 rotation screen-rotation ios ios6

我在iOS 6 SDK中遇到了这个问题:我有一些应该被允许轮换的视图(例如视频视图),而有些则不允许.现在我明白我必须检查应用程序的Info.plist中的所有方向,然后在每个ViewController中进行排序,应该发生什么.但它不起作用!应用程序始终旋转到Info.plist中给出的方向.

Info.plist的:

<key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
Run Code Online (Sandbox Code Playgroud)

任何不允许旋转的ViewController:

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

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

观察:App旋转到横向和纵向.任何想法为什么或我做错了什么?

干杯,马克

编辑:我的最新调查结果还表明,如果您希望在应用程序中的某个位置进行旋转,必须激活项目设置或Info.plist中的所有四个旋转方向.替代方法是覆盖

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
Run Code Online (Sandbox Code Playgroud)

在你的AppDelegate中,它会覆盖Info.plist.不再可能只在Info.plist中设置Portrait,然后通过覆盖shouldAutorotateToInterfaceOrientation或supportedInterfaceOrientations在某些ViewController中进行旋转.

CSm*_*ith 31

如果您的ViewController是UINavigationController或UITabBarController的子级,那么它就是您的问题的父级.您可能需要子类化该父视图控制器,只是覆盖您在问题中显示的那些InterfaceOrientation方法

编辑:

仅限肖像TabBarController的示例

           @interface MyTabBarController : UITabBarController
            {
            }
            @end

            @implementation MyTabBarController

            // put your shouldAutorotateToInterfaceOrientation and other overrides here        
            - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
                return (interfaceOrientation == UIInterfaceOrientationPortrait);
            }

            - (NSUInteger)supportedInterfaceOrientations{ 
                return UIInterfaceOrientationMaskPortrait; 
            } 

        @end
Run Code Online (Sandbox Code Playgroud)

  • 并且你不应该忘记 - supportedInterfaceOrientations方法,因为在iOS6中,shouldAutorotate ...不再使用了! (4认同)

小智 27

除了上面的CSmith的答案之外,UINavigationController子类中的以下代码允许以我期望它首先工作的方式委托给顶视图控制器:

- (BOOL)shouldAutorotate;
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)])
        return [[self topViewController] supportedInterfaceOrientations];
    else
        return [super supportedInterfaceOrientations];
}
Run Code Online (Sandbox Code Playgroud)

  • 这很完美!! 为什么这不是由Apple IOS SDK完成的?:( (5认同)
  • @flypig,曾经是.Apple iOS SDK的行为与之前完全相同.他们在iOS 6中没有明显原因将其更改为新系统. (5认同)

Gle*_*idt 8

这是CSmith方法的另一种替代方案.

如果你要复制的预iOS 6的行为,其中在导航堆栈/标签栏的所有意见必须在允许的组取向的同意,把这个在你的子类UITabBarControllerUINavigationController:

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger orientations = [super supportedInterfaceOrientations];

    for (UIViewController *controller in self.viewControllers)
        orientations = orientations & [controller supportedInterfaceOrientations];

    return orientations;
}
Run Code Online (Sandbox Code Playgroud)