ios - 应用忽略 supportedInterfaceOrientations

标签 ios swift

我有一个带有多个 View Controller 的应用程序,其中大部分应该能够有任何方向,但一个必须是纵向的。但是,应用程序忽略了我从 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
}

当我在横向兼容的 View Controller 中转到本应为纵向的 View Controller 时,没有任何反应 - 应用程序不会自动旋转到纵向。

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

最佳答案

你可以在 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?)
    }
}

还在“方向”的“模拟指标”下的 Storyboard 中设置方向。您还可以看到这个 Stackoverflow's post.

关于ios - 应用忽略 supportedInterfaceOrientations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40677004/

相关文章:

ios - iOS应用的黑 'background color'可以改吗?

ios - 如何在 Eureka Forms 中使用一行来呈现 viewController

ios - 在 iOS (Swift) 中通过 AVAudioPlayerNode 播放缓冲区时出现可听故障 * 在模拟器中工作,但在设备上不工作

iOS (cs193p) : Why are my UISplitViewControllerDelegate delegate methods not called on orientation change?

ios - UIButton 的 Objective-C 数组(包括处理初始化、操作等)

ios 解析_user 指针等于用户id?

ios - swift : Dropbox image upload fails due to RegEX pattern match failing

swift - 如何在 Realm 数据库中快速写入和读取 UImage 形式的数据

xcode - 如何仅在链接时有条件地使用静态库

ios - 从相机 View 截取屏幕截图