简体   繁体   English

如何使用 Swift IOS 以编程方式创建布局和约束

[英]How to programmatically create layout and constraints using Swift IOS

How do you programmatically create layout and constraints using Swift IOS.如何使用 Swift IOS 以编程方式创建布局和约束。 I'm familiar with storyboards, however I'm not sure how to use code to make ui elements.我对故事板很熟悉,但是我不确定如何使用代码来制作 ui 元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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