ios - 以编程方式更改 iOS 应用程序的方向
问题描述
当某事发生时,我如何以编程方式更改我的 iOS 应用程序的方向,例如当我按下按钮时,我需要我的设备去横向,并且在下次单击它时返回纵向。我尝试了这段代码,但什么也没发生
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
任何人都可以帮助我吗?
解决方案
您可以将以下代码添加到 @IBAction 或您喜欢处理按钮按下的方式中
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.setAnimationsEnabled(false)
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIView.setAnimationsEnabled(true)
}
推荐阅读
- java - 设置按钮背景和颜色
- angular - Angular Material双重绑定ngModel不适用于输入文本区域和mat-slide
- r - 参数未在带有过滤器的函数中实现,但没有错误消息
- javascript - vue app中重新计算srcset,元素绑定
- ruby-on-rails - 手动更新密码 Rails Devise
- http - Mapbox 距离矩阵 API 返回全零
- javascript - 我正在尝试创建一个下拉菜单,该菜单存储餐厅菜单中的项目,使用 vanilla JavaScript 通过将对象数组传递给它
- ruby - 在 Ruby 和 RSpec 中使用 Logger
- c# - WPF 在禁用的 DataGrid 上显示空白行
- httprequest - 是否可以确定网站何时收到请求(客户端)?