Ios 以编程方式创建UIView不会';不能在Xcode 11.1中工作

Ios 以编程方式创建UIView不会';不能在Xcode 11.1中工作,ios,swift,ios13,xcode11,snapkit,Ios,Swift,Ios13,Xcode11,Snapkit,我想用Xcode 11.1中的snapkit创建一个UIView作为容器视图,或者更具编程性,但似乎有问题,我认为苹果已经在Xcode 11.x(iOS 13.x)中更改了UIView,因为我在以前的Xcode版本中很容易做到这一点 在SceneDelegate.swift中(苹果已将窗口变量从AppDelegate.swift移动到SceneDelegate.swift) ViewController.swift class ViewController: UIViewController

我想用Xcode 11.1中的snapkit创建一个UIView作为容器视图,或者更具编程性,但似乎有问题,我认为苹果已经在Xcode 11.x(iOS 13.x)中更改了UIView,因为我在以前的Xcode版本中很容易做到这一点

在SceneDelegate.swift中(苹果已将窗口变量从AppDelegate.swift移动到SceneDelegate.swift)

ViewController.swift

  class ViewController: UIViewController {

    var containerView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.backgroundColor = .green
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .blue
        setUpView()
    }
    
    func setUpView() {
        self.view.backgroundColor = .blue
        self.view.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp.centerX)
            make.centerY.equalTo(self.view.snp.centerY)
        }
    }
}
结果是:


要使用自动布局显示视图,您需要提供所有符合布局引擎要求的
W H X Y
。但是您忽略了
宽度
高度
约束

试试这个:

func setUpView() {
    self.view.backgroundColor = .blue
    self.view.addSubview(containerView)
    containerView.snp.makeConstraints { (make) in
        make.width.height.equalTo(100)
        make.center.equalTo(self.view)
    }
}

您需要设置容器视图的高度和宽度

containerView.heightAnchor.constraint(equalToConstant: 100).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 100).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 100).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 100).isActive = true