Home > Net >  How to programmatically create layout and constraints using Swift IOS
How to programmatically create layout and constraints using Swift IOS

Time:07-08

How do you programmatically create layout and constraints using Swift IOS. I'm familiar with storyboards, however I'm not sure how to use code to make ui elements.

CodePudding user response:

There are three main steps. First you have to define the element, add it to the view, then add constraints.

For example,

let button = UIButton() 
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button) 

NSLayoutConstraint.activate([
        button.heightAnchor.constraint(equalTo: view.heightAnchor)
      
        
])
  • Related