简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM