ChessMaster
ChessMaster

Reputation: 21

How to programmatically create layout and constraints using Swift IOS

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.

Upvotes: 1

Views: 935

Answers (1)

Shiv Nik
Shiv Nik

Reputation: 107

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)
      
        
])

Upvotes: 2

Related Questions