将约束从视图传递到控制器 (MVC)

Pass constraint from View to Controller (MVC)

我正在编写我的第一个 MVC 架构项目。我是初学者,可能会错过一个简单的。但是我就是想不通如何在View中实现约束并传递给Controller。

查看:

import UIKit

class ViewExample: UIView {
    
    var textView: UITextView = {
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.text = "Hello, world!"
        
        return textView
    }()
    
    func setupViews() {
        addSubview(textView)
        
        NSLayoutConstraint.activate([
            textView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 10),
            textView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10),
            textView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
            textView.heightAnchor.constraint(equalTo: textView.widthAnchor, multiplier: 2)
        ])
    }
    
    override init(frame: CGRect){
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
    }
}

控制器:

import UIKit

    class ViewController: UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let ve = ViewExample()
            ve.setupViews()
            view.addSubview(ve)
        }
    
    }

我需要解决什么问题才能正常工作?

您没有为名为 ve 的视图设置约束。我推荐你使用 SnapKit cocoapod。它可以帮助您轻松地为您的视图设置约束。 并像这样更新。

import SnapKit 
class ViewExample: UIView {
....
    func setupViews() {
        addSubview(textView)
        textView.snp.makeConstraints {
            [=10=].edges.equalToSuperview()
            [=10=].height.equalTo(textView.snp.width).multipliedBy(2)
        }
    }
}

import SnapKit 
class ViewController{
    ....
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let ve = ViewExample()
        ve.setupViews()
        view.addSubview(ve)
        ve.snp.makeConstraints {
            [=10=].top.equalToSuperview().offset(20)
            [=10=].left.equalToSuperview().offset(20)
            [=10=].right.equalToSuperview().offset(-20)
        }
    }
    ....
}

如果不想使用SnapKit,在viewDidLoad方法中添加如下代码。 首先,在setupViews方法中对textView添加底部约束。

let ve = ViewExample()
ve.setupViews()
view.addSubview(ve)
ve.translateAutoresizingMaskIntoConstraints = false 
ve.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true 
ve.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true 
ve.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 20).isActive = true 

看来我还分明不知道该如何去表述一个想法。但是多亏了您的回答-我找到了解决方案。原来太简单了..

查看:

class ViewEx: UIView {
    
    lazy var textView: UITextView = {
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.text = "Hello, world!"
        textView.textAlignment = .center
        textView.backgroundColor = .gray
        
        return textView
    }()
    
}

控制器:

    class ViewController: UIViewController {
        
         let ve = ViewEx()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            addSubviews()
            setupConstraint()
        }
        
        func addSubviews() {
            view.addSubview(ve.textView)
        }
    
        func setupConstraint() {
            
            NSLayoutConstraint.activate([
                ve.textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
                ve.textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
                ve.textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
                ve.textView.heightAnchor.constraint(equalTo: ve.textView.widthAnchor, multiplier: 2)
            ])
        }
        
    }