Переопределение shouldAutorotate не работает в Swift 3
Я пытаюсь предотвратить вращение на одном UIViewController
, и я не могу этого добиться.
Я делаю что-то вроде этого:
open override var shouldAutorotate: Bool {
get {
return false
}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .portrait
}
}
И неподвижные кадры UIViewControler
. UIViewController находится внутри открытого UINavigationController.
Я посмотрел много вопросов отсюда, и ни один из ответов не работает для меня.
В Swift 2 я использовал переопределение shouldAutorotate
, но в Swift 3 эта функция больше не существует.
Как я могу сделать это в Swift 3, что я делал в Swift 2?
Ответы
Ответ 1
Я не знаю, почему голосование за закрытие вопроса, если я могу воспроизвести это поведение много раз. UIViewController
находится внутри a UINavigationController
, открытый модально.
Это то, что я сделал для решения проблемы.
Я создаю этот класс, и я установил UINavigationController
, который содержит UIViewController
, который я хочу предотвратить, чтобы вращать
class NavigationController: UINavigationController {
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
И вот оно, оно работает для меня
Ответ 2
Добавьте этот код в AppDelegate.swift
var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
Добавьте к диспетчеру просмотра, который вы хотите принудительно настроить:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//let value = UIInterfaceOrientation.landscapeLeft.rawValue
//UIDevice.current.setValue(value, forKey: "orientation")
AppDelegate.AppUtility.lockOrientation(.landscapeLeft)
}