AutoLayout 코드로 뷰 그리기
import UIKit
class ViewController: UIViewController {
let hiLabel : UILabel = {
let label = UILabel()
label.text = "hi"
return label
}()
let helloLabel : UILabel = {
let label = UILabel()
label.text = "hello"
return label
}()
let centerLabel : UILabel = {
let label = UILabel()
label.text = "center"
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addSubview(hiLabel)
hiLabel.translatesAutoresizingMaskIntoConstraints = false
hiLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
hiLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
hiLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
view.addSubview(helloLabel)
helloLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
helloLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
helloLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
helloLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
helloLabel.topAnchor.constraint(equalTo: hiLabel.bottomAnchor, constant: 10)
])
view.addSubview(centerLabel)
centerLabel.translatesAutoresizingMaskIntoConstraints = false
centerLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
centerLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
댓글 없음:
댓글 쓰기