首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将视图从纵向模式转变为景观模式,并锁定它?

如何将视图从纵向模式转变为景观模式,并锁定它?
EN

Stack Overflow用户
提问于 2017-05-04 02:54:11
回答 1查看 872关注 0票数 0

我有一个电影视图压缩在右下视图与肖像模式。当用户展开电影视图时,电影视图将在景观模式下扩展到全屏。我也想锁定电影视图到景观模式时,全屏,无论设备是什么方向。

注意:我所有的其他视图都处于纵向模式。

我用这段代码引用了这个post

应用程序设置

AppDelegate.swift

代码语言:javascript
复制
internal var shouldRotate = false

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

视图控制器,

代码语言:javascript
复制
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
}

日志,

代码语言:javascript
复制
false
UIInterfaceOrientationMask(rawValue: 26)
true
UIInterfaceOrientationMask(rawValue: 26)
false
UIInterfaceOrientationMask(rawValue: 26)

在设置方向之前,我将shouldRotate设置为true,使视图可以更改为景观模式。在设置方向之后,我将shoudRotate设置为false以禁用旋转。然后我测试它,当我点击按钮,电影视图改变为景观,在它旋转我的设备后,电影视图改变为纵向,锁定到纵向模式,而不是景观模式。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-10 02:52:46

它是由这个功能引起的,

代码语言:javascript
复制
func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}

.allButUpsideDown更改为.landscape即可。

可行的代码,

AppDelegate.swift

代码语言:javascript
复制
func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .landscape : .portrait
}

视图控制器,

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43773075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档