如何在SWIFT中將單視圖控制器的方向鎖定為縱向模式因為我的應用程序得到了所有方向的支持。我只想鎖定特定UIViewController的縱向模式。(例如,假設它是選項卡式應用程序,并且當signin View以模態方式出現時,我只希望該signin View只有在用戶如何旋轉設備或當前設備方向的情況下才能進入縱向模式)
3 回答
胡說叔叔
TA貢獻1804條經驗 獲得超8個贊
SWIFT 3&4
/// set orientations you want to be allowed in this property by defaultvar orientationLock = UIInterfaceOrientationMask.allfunc a
pplication(_ 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 }
}
/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}} override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppUtility.lockOrientation(.portrait)
// Or to rotate and lock // AppUtility.lockOrientation(.portrait, andRotateTo: .
portrait)}override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Don't forget to reset when view is being removed AppUtility.lockOrientation(.all)}如果iPad或通用應用程序
supportedInterfaceOrientationsFor
SMILET
TA貢獻1796條經驗 獲得超4個贊
SWIFT 4
var orientationLock = UIInterfaceOrientationMask.allfunc 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 viewWillAppear(_ animated: Bool) {AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo:
UIInterfaceOrientation.portrait)
}override func viewWillDisappear(_ animated: Bool) {
AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.all)
}
弒天下
TA貢獻1818條經驗 獲得超8個贊
override func viewDidLoad() {
super.viewDidLoad()
// Force the device in portrait mode when the view controller gets loaded UIDevice.currentDevice().
setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") }override func shouldAutorotate() -> Bool {
// Lock autorotate return false}override func supportedInterfaceOrientations() -> Int {
// Only allow Portrait return Int(UIInterfaceOrientationMask.Portrait.rawValue)}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
// Only allow Portrait return UIInterfaceOrientation.Portrait}func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All}- 3 回答
- 0 關注
- 478 瀏覽
添加回答
舉報
0/150
提交
取消
