首页 文章

iOS 6轮换:supportedInterfaceOrientations不起作用?

提问于
浏览
36

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

Info.plist的:

<key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>

任何不允许旋转的ViewController:

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

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

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

干杯,马克

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

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

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

9 回答

  • 8

    如果您的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
    
  • 0

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

    - (BOOL)shouldAutorotate;
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)])
            return [[self topViewController] supportedInterfaceOrientations];
        else
            return [super supportedInterfaceOrientations];
    }
    
  • 2

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

    如果要复制导航堆栈/选项卡栏中的所有视图必须就允许的方向集达成一致的iOS 6之前的行为,请将其放在 UITabBarControllerUINavigationController 的子类中:

    - (NSUInteger)supportedInterfaceOrientations
    {
        NSUInteger orientations = [super supportedInterfaceOrientations];
    
        for (UIViewController *controller in self.viewControllers)
            orientations = orientations & [controller supportedInterfaceOrientations];
    
        return orientations;
    }
    
  • 0

    尝试添加此类别:

    @interface UINavigationController(InterfaceOrientation)
    
    @end
    
    @implementation UINavigationController(InterfaceOrientation)
    
    - (NSUInteger) supportedInterfaceOrientations {
        if (self.viewControllers.count > 0)
            return [[self.viewControllers objectAtIndex:0] supportedInterfaceOrientations];
        else
            return UIInterfaceOrientationMaskAll;
    }
    
    @end
    
  • -1

    对于使用UINavigationController和Swift的人,您可以将此扩展添加到项目中 . 之后,导航控制器将控件委托给其子控制器 .

    extension UINavigationController {
        override public func supportedInterfaceOrientations()
        -> UIInterfaceOrientationMask {
            if let ctrl = topViewController {
                return ctrl.supportedInterfaceOrientations()
            }
            return super.supportedInterfaceOrientations()
        }
    
        override public func shouldAutorotate() -> Bool {
            if let ctrl = topViewController {
                return ctrl.shouldAutorotate()
            }
            return super.shouldAutorotate()
        }
    }
    
  • -2

    @CSmith和@EvanSchoenberg的进一步补充 .

    如果您有一些旋转的视图,而某些视图没有旋转,您必须创建 UITabBarController 的自定义实例,但仍然让每个 UIViewController 决定 .

    - (BOOL)shouldAutorotate;
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        UIViewController * top;
        UIViewController * tab = self.selectedViewController;
        if([tab isKindOfClass:
            ([UINavigationController class])]) {
            top = [((UINavigationController *)tab)
                     topViewController];
        }
    
        if ([top respondsToSelector:@selector(supportedInterfaceOrientations)])
            return [top supportedInterfaceOrientations];
        else
            return [super supportedInterfaceOrientations];
    }
    
  • 1

    @ Alvivi的答案更新为 Swift 4 .

    extension UINavigationController {
    
        // Look for the supportedInterfaceOrientations of the topViewController
        // Otherwise, viewController will rotate irrespective of the value returned by the ViewController
        override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            if let ctrl = self.topViewController {
                return ctrl.supportedInterfaceOrientations
            }
            return super.supportedInterfaceOrientations
        }
    
        // Look for the shouldAutorotate of the topViewController
        // Otherwise, viewController will rotate irrespective of the value returned by the ViewController
        override open var shouldAutorotate: Bool {
            if let ctrl = self.topViewController {
                return ctrl.shouldAutorotate
            }
            return super.shouldAutorotate
        }
    }
    
  • 31

    @Shimanski Artem的答案很好,但我认为使用最顶层(当前可见)控制器是更好的解决方案:

    @interface UINavigationController(InterfaceOrientation)
    
    @end
    
    @implementation UINavigationController(InterfaceOrientation)
    
    - (NSUInteger) supportedInterfaceOrientations {
        if (self.viewControllers.count > 0){
            return [[self.viewControllers objectAtIndex:[self.viewControllers count] - 1] supportedInterfaceOrientations];
        }
        return UIInterfaceOrientationMaskAll;
    }
    
    @end
    
  • 27

    作为备用选项,如果您想在应用中保留iOS6之前的旋转功能:

    这是github上的一些有用的代码,可以调用iOS6的方法调用,以便旋转像在iOS4 / iOS4上一样 . 这真的对我有所帮助,因为我正在支持一个真正微观管理轮换的遗留应用程序 . 实现iOS6所需的更改需要做很多工作 . 感谢发布它的用户 .

    https://gist.github.com/3725118

相关问题