Swift 如何使Xcode游乐场旋转到景观
我试图测试一个景观,但到目前为止,我无法取得进展。我的代码如下所示:Swift 如何使Xcode游乐场旋转到景观,swift,swift-playground,screen-rotation,Swift,Swift Playground,Screen Rotation,我试图测试一个景观,但到目前为止,我无法取得进展。我的代码如下所示: import UIKit import PlaygroundSupport class MyViewController : UIViewController { override func loadView() { let view = UIView() view.backgroundColor = .white let label = UILabel() label.text =
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(TooOrientation:)
的调用时,结果是:
并且,当我在视图中取消注释SimulatedDeviceRotation(TooOrientation:)
时,结果是:
当然,我希望看到第二个结果,但是第一个结果是水平旋转的。你能给我指一下正确的方向吗?我错过了一些东西,但我一直找不到。不要搞乱方向,你不会成功的
将代码的最后一行更改为:
var myViewController = MyViewController()
myViewController.preferredContentSize = CGSize(width: 668, height: 375)
PlaygroundPage.current.liveView = myViewController
并删除在加载视图
,视图显示
中处理设备方向的所有内容,并删除方法模拟总督
更新
如果将任意值设置为preferredContentSize
,则会遇到约束问题或更糟的情况,如视图在实时视图中被替换
我所做的:首先读取当前内容大小的默认值:
print(myViewController.preferredContentSize)
对我来说,这是375.0的宽度和668.0的高度
因此,只需将此值替换为新的preferredContentSize
,一切都应该正常