Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使Xcode游乐场旋转到景观_Swift_Swift Playground_Screen Rotation - Fatal编程技术网

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
,一切都应该正常