How to make a custom view resize with the window with Cocoa Auto Layout?

后端 未结 4 1224
野趣味
野趣味 2020-12-12 08:24

I have a single window with a single custom view in it, and I want the custom view to resize with the window so that it entirely fills it at any time. If I write:

         


        
4条回答
  •  -上瘾入骨i
    2020-12-12 09:09

    You can create constraints with Layout Anchors with very easy to read format:

    Swift2 code

    func fit(childView: UIView, parentView: UIView) {
        childView.translatesAutoresizingMaskIntoConstraints = false
        childView.topAnchor.constraintEqualToAnchor(parentView.topAnchor).active = true
        childView.leadingAnchor.constraintEqualToAnchor(parentView.leadingAnchor).active = true
        childView.trailingAnchor.constraintEqualToAnchor(parentView.trailingAnchor).active = true
        childView.bottomAnchor.constraintEqualToAnchor(parentView.bottomAnchor).active = true
    }
    

    Use:

    parrentView.addSubview(childView)
    fit(childView, parentView: parrentView)
    

提交回复
热议问题