首页 文章

当应用程序在iOS 8中启动时,如果设备处于横向方向,如何强制纵向方向

提问于
浏览
0

我的应用程序允许所有四个方向,但rootviewcontroller应该只允许纵向方向 .

我覆盖了rootviewcontroller中的supportedInterfaceOrientations方法,但是如果设备在应用程序启动时以横向方向保持,则视图控制器在横向方向上显示不正确,即使只允许纵向方向 . 这是iOS 8特定问题 .

override func supportedInterfaceOrientations() -> Int {
    return UIInterfaceOrientation.Portrait.rawValue
}

2 回答

  • 0

    在ViewController中:

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    Swift版本:

    override func shouldAutorotate() -> Bool {
        return true
    }
    override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }
    
  • 1

    对我来说,实现AppDelegate函数应用程序supportedInterfaceOrientationsForWindow就可以了

    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
    
        if let viewController = self.window?.rootViewController?.presentedViewController as? PortraitViewController{
    
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
        }else{
    
            return Int(UIInterfaceOrientationMask.All.rawValue)
        }
    
    }
    

    其中PortraitViewController应替换为根视图控制器类的名称

相关问题