【问题标题】:App ignores supportedInterfaceOrientations应用忽略supportedInterfaceOrientations
【发布时间】:2016-11-18 12:20:33
【问题描述】:

我有一个带有多个视图控制器的应用程序,其中大多数应该能够有任何方向,但一个必须是纵向的。但是,应用程序忽略了我从supportedInterfaceOrientations() 返回的值

我的代码:

UINavigationController subclass:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if let topViewController = self.topViewController {
        return topViewController.supportedInterfaceOrientations()
    }

    return [.Landscape, .Portrait]
}

override func shouldAutorotate() -> Bool {
    if let topViewController = self.topViewController {
        return topViewController.shouldAutorotate()
    }

    return true
}


UIViewController subclass

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Portrait
}

override func shouldAutorotate() -> Bool {
    return true
}

当我在一个横向兼容的视图控制器中并转到应该是纵向的视图控制器时,什么也没有发生 - 应用程序不会自动旋转到纵向。

我也从 info.plist 中删除了支持的界面方向。有什么想法吗?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    你可以在AppDelegate.swift中设置它们,在这个函数里面

    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
    
        return checkOrientationForVC(self.window?.rootViewController?)
    
    }
    

    在这个函数中你可以做类似的事情:

       func checkOrientationForVC(viewController:UIViewController?)-> Int{
    
        if(viewController == nil){
    
            return Int(UIInterfaceOrientationMask.All.rawValue)//All means all orientation
    
        }else if (viewController is SignInViewController){ //Your View controller here
    
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    
        }else{
    
            return checkOrientationForVC(viewController!.presentedViewController?)
        }
    }
    

    在“方向”的“模拟指标”下也可以在情节提要中设置方向。您也可以看到这个Stackoverflow's post.

    【讨论】:

    • 这有一点帮助,但并不完全——它仍然不会自动旋转。当我切换到所需的视图控制器时,它仍然是横向的,但是当我旋转设备时,它会锁定纵向。 shouldAutorotate() 在视图控制器和导航控制器中都返回 true
    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多