swiftuiviewnslayoutconstraintvarlayoutmargins

How to apply multiplier to a var bottom constraint


My code below produces the image below.

enter image description here

What I want is the height of the UIView to be exactly half of the view controller user something like:

constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, multiplier: 1/2)

I dont know where to put it since I am creating a var for every side.

import UIKit

class ViewController: UIViewController {

    var myView = UIView()
    var topConstraint: NSLayoutConstraint!
    var bottomConstraint: NSLayoutConstraint!
    var leadingConstraint: NSLayoutConstraint!
    var trailingConstraint: NSLayoutConstraint!
    var jessicaAlba:Float = 50

    override func viewDidLoad() {
        super.viewDidLoad()

        myView.translatesAutoresizingMaskIntoConstraints = false
        myView.backgroundColor = UIColor.orange
        view.addSubview(myView)

        topConstraint = myView.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
        topConstraint.isActive = true
        bottomConstraint = myView.bottomAnchor.constraint(equalTo: view.bottomAnchor , constant: CGFloat(-jessicaAlba))
        bottomConstraint.isActive = true

        leadingConstraint = myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
        leadingConstraint.isActive = true
        trailingConstraint = myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
        trailingConstraint.isActive = true
    }
}

Solution

  • You should use heightConstraint instead of bottomConstraint to get that done. Try the snippet below.

    import UIKit
    
    class ViewController: UIViewController, UITextFieldDelegate {
        var myView = UIView()
        var topConstraint: NSLayoutConstraint!
        var heightConstraint: NSLayoutConstraint!
        var leadingConstraint: NSLayoutConstraint!
        var trailingConstraint: NSLayoutConstraint!
        var jessicaAlba:Float = 50
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            myView.translatesAutoresizingMaskIntoConstraints = false
            myView.backgroundColor = UIColor.orange
            view.addSubview(myView)
    
            topConstraint = myView.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
            topConstraint.isActive = true
            heightConstraint = myView.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.5, constant: CGFloat(-jessicaAlba))
            heightConstraint.isActive = true      
    
            leadingConstraint = myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
            leadingConstraint.isActive = true
            trailingConstraint = myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
            trailingConstraint.isActive = true 
        }
    }