在Dynamic ViewController中以编程方式对自定义UIView进行约束

时间:2017-11-22 12:36:01

标签: ios swift uiview constraints programmatically

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

View with lightGray BG

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

顶部:0

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

领先:0

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

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

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

示例:

enter image description here

这是我的代码:

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。

我做错了什么???

1 个答案:

答案 0 :(得分:1)

你应该只指定底部OR高度和宽度或尾随,否则你将在这里遇到冲突。

参见playground:

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