以编程方式约束动态中的自定义 UIView ViewController

Constraints programmatically for custom UIView in Dynamic ViewController

我正在使用此代码动态创建新的 UIViewController

@IBAction func newVCBtnPressed(_ sender: Any) {
            let controller = DynamicVC()
            show(controller, sender: sender)
    }

在新的 UIViewController 中,我使用此代码创建新的 UIView:

override func loadView() {

        view = UIView()
        view.backgroundColor = .lightGray
}

结果我有 view.lightGray 背景色。

我想添加自定义 UIView 并以编程方式设置约束,结果我希望 UIView 具有以下约束:

顶部:0

底部:(view.frame.height*0.9)

前导:0

尾随:(view.frame.width*0.15)

宽度:(view.frame.width*0.85)

身高:(view.frame.height*0.1)

示例:

这是我的代码:

topMenuView = UIView()
        topMenuView.backgroundColor = .red

        view.addSubview(topMenuView)
        topMenuView.translatesAutoresizingMaskIntoConstraints = false
         setupConstraints(item: topMenuView, topC: 0, topToItem: view, bottomC: (view.frame.height*0.9), bottomToItem: view, widthC: (view.frame.width*0.85), heightC: (view.frame.height*0.1), leadingCon: 0, trailingCon: (view.frame.width*0.15))

我将这个构造函数用于约束:

 func setupConstraints(item:UIView, topC:CGFloat, topToItem:UIView, bottomC:CGFloat, bottomToItem:UIView, widthC:CGFloat, heightC:CGFloat, leadingCon:CGFloat, trailingCon:CGFloat) {

            let topConstraint = NSLayoutConstraint(item: item, attribute: .top, relatedBy: .equal, toItem: topToItem, attribute: .bottom, multiplier: 1, constant: topC)
            let bottomConstraint = NSLayoutConstraint(item: item, attribute: .bottom, relatedBy: .equal, toItem: bottomToItem, attribute: .top, multiplier: 1, constant: bottomC)
            let widthConstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: widthC)
            let heightConstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: heightC)

            let leading = NSLayoutConstraint(item: item,attribute: .leading,relatedBy: .equal, toItem: view, attribute: .leadingMargin, multiplier: 1.0, constant: leadingCon)

            let trailing = NSLayoutConstraint(item: item,attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailingMargin,multiplier: 1.0,constant: trailingCon)

            view?.addConstraints([topConstraint, bottomConstraint, widthConstraint, heightConstraint, leading, trailing])

            NSLayoutConstraint.activate([topConstraint, bottomConstraint, widthConstraint, heightConstraint, leading, trailing])
        }

但结果我只收到灰色背景的 UIView,没有出现红色背景的新 UIView。

我哪里做错了???

你应该只指定 bottom OR height and width OR trailing,否则你会在这里发生冲突。

看游乐场:

import PlaygroundSupport
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let red = UIView()
        red.backgroundColor = .red
        view.addSubview(red)
        red.translatesAutoresizingMaskIntoConstraints = false
        red.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        red.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        red.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.85).isActive = true
        red.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1).isActive = true
    }
}

PlaygroundPage.current.liveView = ViewController()