После создания ярлыка, кнопки и т. Д. Я использую функцию для макета.Вызовите функцию в viewdidload, и она работает очень хорошо.Но я не доволен кодами NSLayoutConstraint.Как уменьшить этот код?Можно ли использовать для этого расширение?
iphne 5s и Xs
override func viewDidLoad() {
super.viewDidLoad()
buildUI()
}
private func buildUI() {
view.backgroundColor = .lightGray
view.addSubview(contentView)
view.addSubview(usernameTextField)
view.addSubview(passwordTextField)
view.addSubview(forgotPasswordButton)
view.addSubview(loginButton)
view.addSubview(createAccountButton)
NSLayoutConstraint.activate([
contentView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 32),
contentView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -32),
contentView.heightAnchor.constraint(equalToConstant: 354),
contentView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
usernameTextField.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 32),
usernameTextField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -32),
usernameTextField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 24),
usernameTextField.heightAnchor.constraint(equalToConstant: 50),
passwordTextField.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 32),
passwordTextField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -32),
passwordTextField.topAnchor.constraint(equalTo: usernameTextField.topAnchor, constant: 64),
passwordTextField.heightAnchor.constraint(equalToConstant: 50),
forgotPasswordButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
forgotPasswordButton.topAnchor.constraint(equalTo: passwordTextField.topAnchor, constant: 70),
loginButton.bottomAnchor.constraint(equalTo: createAccountButton.bottomAnchor, constant: -46),
loginButton.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 32),
loginButton.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -32),
loginButton.heightAnchor.constraint(equalToConstant: 50),
createAccountButton.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
createAccountButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -30),
])
}