Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift iOS Layout Constraint. Using Constant that is proportional to view height programatically

I am laying out a view programatically in Swift for iOS, but struggling to get my constraint quite how I want it. This is what I currently have:

NSLayoutConstraint.activate([
            Logo.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 30),
            Logo.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -30),
            Logo.heightAnchor.constraint(equalToConstant: 55),
            Logo.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 150),
        ])

This is fine on large screens but as the screen gets smaller I want to close the space between the Logo and the label. Currently this is set to a fixed constant of 150. What I would like to do is use a multiplier here that is based on the view height (or something similar) but I can not figure that out. How should I define the constraint to do this? Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try

/* Play with percent as you need also you can check current 
     device type iphone/ipad and set it accordingly  */

let height = UIScreen.main.bounds.height * 0.25 

 logo.topAnchor.constraint(equalTo: label.bottomAnchor, constant: height),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading