我正在尝试使用以下代码在iPad Playgrounds中显示此简单的ViewController:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
let view1 = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width/2, height: 100))
view1.backgroundColor = .red
view.addSubview(view1)
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
您可以看到,即使View1的框架显示为view.frame.width / 2,该视图仍然如下所示:
怎么了?谢谢。
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以在初始化ViewController之后设置首选大小:
import UIKit
import PlaygroundSupport
class VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
let view1 = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width/2, height: 100))
view1.backgroundColor = .red
view.addSubview(view1)
}
}
let vc = VC()
vc.preferredContentSize = CGSize(width: 768, height: 1024)
PlaygroundPage.current.liveView = vc