如何将视图从纵向模式更改为横向模式并锁定它? [英] how to change a view from portrait mode to landscape mode and lock it?

查看:253
本文介绍了如何将视图从纵向模式更改为横向模式并锁定它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用纵向模式在右下角的视口中压缩了电影视图.当用户展开影片视图时,影片视图将在横向模式下扩展到全屏.我还想在全屏模式下将电影视图锁定为横向模式,无论设备的方向如何.

I have a movie view compressed at the right-bottom viewport with portrait mode. And the movie view will expand to full screen in landscape mode when user expand the movie view. I also want to lock the movie view to landscape mode when full screen no matter what orientation the device is.

注意:我所有其他视图均为纵向模式.

Note: all my other views are in portrait mode.

我已将此代码参考帖子

应用设置

AppDelegate.swift

AppDelegate.swift

internal var shouldRotate = false

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}

视图控制器,

func expandPlayerWindow(button: UIButton) {
    self.player.view.frame = CGRect(x:0, y:-20, width: UIScreen.main.bounds.maxY, height: UIScreen.main.bounds.maxX)
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    print(appDelegate.shouldRotate)
    print(self.supportedInterfaceOrientations)
    appDelegate.shouldRotate = true // or false to disable rotation
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    print(appDelegate.shouldRotate)
    print(self.supportedInterfaceOrientations)
    appDelegate.shouldRotate = false
    print(appDelegate.shouldRotate)
    print(self.supportedInterfaceOrientations)
    UIApplication.shared.isStatusBarHidden = false
}

日志

false
UIInterfaceOrientationMask(rawValue: 26)
true
UIInterfaceOrientationMask(rawValue: 26)
false
UIInterfaceOrientationMask(rawValue: 26)

在设置方向之前,我将shouldRotate设置为true,这使得视图可以更改为横向模式.设置方向后,我将shoudRotate设置为false以禁用旋转.然后我对其进行测试,当我单击按钮时,电影视图将变为横向,在旋转设备后,电影视图将变为纵向,并锁定为纵向模式而不是横向模式.

I set shouldRotate to true before setorientation, this make view can change to landscape mode. And after set orientation I set shoudRotate to false to disable rotation. Then I test it, when I click the button, the movie view change to landscape, after it I rotate my device the movie view change to portrait, and locked to the portrait mode not the landscape mode.

推荐答案

由此功能引起

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}

.allButUpsideDown更改为.landscape即可.

可行代码

AppDelegate.swift

AppDelegate.swift

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .landscape : .portrait
}

视图控制器,

func expandPlayerWindow() {
    self.player.view.frame = CGRect(x:0, y:-20, width: UIScreen.main.bounds.maxY, height: UIScreen.main.bounds.maxX)
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.shouldRotate = true // or false to disable rotation
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    appDelegate.shouldRotate = true
    UIApplication.shared.isStatusBarHidden = true
}

这篇关于如何将视图从纵向模式更改为横向模式并锁定它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆