swift 动态视图控制器中自定义UIView的编程约束

dldeef67  于 2023-05-05  发布在  Swift
关注(0)|答案(1)|浏览(120)

我正在使用以下代码动态创建新的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 backgroundcolor。

我想添加自定义UIView并以编程方式设置约束,结果我希望UIView具有以下约束:
最大值:0
底部:(视图、框架、高度 0.9)
前导:0
trailing:(view.frame.width
0.15)
width:(view.frame.width*0.85)
高度:(视图.框架.高度 *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没有出现。
我做错了什么???

ht4b089n

ht4b089n1#

你应该只指定底部或高度和宽度或尾部,否则你会在这里得到冲突。
看操场:

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()

相关问题