【发布时间】:2018-02-27 11:22:52
【问题描述】:
我正在尝试测试横向视图,但到目前为止我无法取得进展。我的代码如下所示:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.text = "Hallo"
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
self.view = view
NSLayoutConstraint.activate([label.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
label.centerXAnchor.constraint(equalTo: view.centerXAnchor)])
simulateDeviceRotation(toOrientation: .landscapeLeft)
}
override func viewDidAppear(_ animated: Bool) {
//simulateDeviceRotation(toOrientation: .landscapeLeft)
}
}
// Present the view controller in the Live View window
func simulateDeviceRotation(toOrientation orientation: UIDeviceOrientation) {
let orientationValue = NSNumber(value: orientation.rawValue)
UIDevice.current.setValue(orientationValue, forKey: "orientation")
}
PlaygroundPage.current.liveView = MyViewController()
当我在loadView() 中取消注释对simulateDeviceRotation(toOrientation:) 的调用时,结果是:
而且,当我在 viewDidAppear(_:) 中取消注释 simulateDeviceRotation(toOrientation:) 时,结果是:
当然,我希望看到第二个结果,但要使用第一个的水平旋转。你能指出我正确的方向吗?我错过了一些东西,但我找不到它。
【问题讨论】:
标签: swift swift-playground screen-rotation