Pass constraint from View to Controller (MVC)

egorskikh

I am writing my first project on MVC architecture. I'm a beginner and might be missing out on a simple one. But I just can't figure out how to implement constraint in View and deliver it to Controller.

View:

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

Controller:

import UIKit

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

What do I need to fix for this to work?

Li Jin

You didn't set the constraints for the view named ve. I recommend you to use SnapKit cocoapod. it helps you easy to set constraints for your views. And update like this.

import SnapKit 
class ViewExample: UIView {
....
    func setupViews() {
        addSubview(textView)
        textView.snp.makeConstraints {
            $0.edges.equalToSuperview()
            $0.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 {
            $0.top.equalToSuperview().offset(20)
            $0.left.equalToSuperview().offset(20)
            $0.right.equalToSuperview().offset(-20)
        }
    }
    ....
}

if you don't want to use SnapKit, add the following code to the viewDidLoad method. First, add bottom constraint to textView in setupViews method.

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 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

value not pass from view to controller in mvc

Pass object from view to controller MVC 5

Pass a list from a view to a controller, MVC 5

mvc-pass data from controller to view of other controller

how to pass Html.HiddenFor from view to view and controller mvc

How to pass data from datepicker in view to controller in mvc

How to pass a variables data from a controller to a view (MVC)

Add item into List from View and pass to Controller in MVC5

ASP.Net MVC How to pass data from view to controller

How to pass row id by href link in mvc from view to controller

C# MVC pass parameters from View to Controller using dotnetfiddle

How to pass model from MVC view to angular controller?

pass a model from partial view to controller by ajax in mvc

Pass Html String from Controller to View ASP.Net MVC

ASP.NET MVC: Pass list of objects from Controller to View

ASP.NET MVC: Pass complex data from View to Controller

MVC 4 how pass data correctly from controller to view

How to pass List from view to controller - MVC 4

How to pass model between different method of controller from view MVC

How to Pass value from view to another controller in MVC ASP .NET

Pass Collection of Object from Controller to View Asp.Net MVC

How to Pass Multiple Objects from Controller action to View in MVC?

.NET MVC - pass database table from view to controller

MVC 5 pass model from view to controller using actionlink

MVC pass int from view to controller with a button click

Call controller from view and pass value from view to controller in asp.net mvc

Pass data from View to Controller

pass json from controller to view

Pass Model from View to Controller