表达式类型'@lvalue CGRect'不明确,没有更多上下文

时间:2019-03-24 10:41:04

标签: ios swift

我正在尝试使用快速游乐场创建某些内容,并且在自动布局方面遇到了一些困难。这是我的代码

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'是模棱两可的,没有更多上下文

谢谢!

1 个答案:

答案 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:)这样的方法。