![](/img/trans.png)
[英]How to present a view controller in portrait or landscape, in application that is portrait-only
[英]Allow only 1 view controller to be in landscape or portrait
我的應用程序是人像,我想做的是允許1個視圖控制器為人像或風景,我嘗試在AppDelegate中使用此代碼,但出現錯誤
func application(_ application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {
let currentViewController: UIViewController? = self.topView
// Get topmost/visible view controller
var currentViewController: UIViewController? = self.topViewController
// Check whether it implements a dummy methods called canRotate
if currentViewController?.responds(to: #selector(self.canRotate)) {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown
}
// Only allow portrait (standard behaviour)
return .portrait
}
}
有誰知道如何迅速做到這一點?
我在我的應用中采用了以下方法。
使用以下代碼創建一個自定義UINavigationController,並確保您的根導航控制器已更新為將其用作其類。
import UIKit
class NavigationController: UINavigationController {
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
if let _ = presentedViewController as? MyPortraitViewController {
return .portrait
}
return .allButUpsideDown
}
}
您可以將MyPortraitViewController
替換為要偏離全局設置的視圖控制器的類名。
雖然在此示例中使用.portrait
,但是您可以自由使用其他選項。
一個實現示例是替換情節提要中基本導航控制器的類,如下所示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.