我正在尝试使用快速游乐场创建某些内容,并且在自动布局方面遇到了一些困难。这是我的代码
import PlaygroundSupport
import UIKit
class ViewController: UIViewController {
var gameVC = GameView()
override func viewDidLoad() {
super.viewDidLoad()
gameVC.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(gameVC)
gameVC.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
gameVC.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
gameVC.widthAnchor.constraint(equalToConstraint: view.frame.width).isActive = true
gameVC.heightAnchor.constraint(equalToConstraint: view.frame.height).isActive = true
}
}
我在网上遇到错误
gameVC.widthAnchor.constraint(equalToConstraint: view.frame.width).isActive = true
说
表达类型'@lvalue CGRect'是模棱两可的,没有更多上下文
谢谢!
答案 0 :(得分:3)
view.frame.width
不是约束。你可能想要
gameVC.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
gameVC.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
或者也许:
gameVC.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
gameVC.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
我实际上认为没有所谓的constraint(equalToConstraint:)
这样的方法。