translatesAutoresizingMaskIntoConstraints

67 篇文章 7 订阅 ¥49.90 ¥99.00
translatesAutoresizingMaskIntoConstraints属性和autolayout有关,
如果你定义的view想用autolayout,
就将translatesAutoresizingMaskIntoConstraints设为NO,
如果你使用的不是autolayout,
就将translatesAutoresizingMaskIntoConstraints设为YES,
对应关系如下
autolayout       translatesAutoresizingMaskIntoConstraints    
是                                NO    
否                                YES

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的注册界面的 Swift 5 代码: ```swift import UIKit class RegisterViewController: UIViewController { let titleLabel: UILabel = { let label = UILabel() label.text = "注册" label.font = UIFont.boldSystemFont(ofSize: 36) label.translatesAutoresizingMaskIntoConstraints = false return label }() let emailTextField: UITextField = { let textField = UITextField() textField.placeholder = "邮箱" textField.borderStyle = .roundedRect textField.translatesAutoresizingMaskIntoConstraints = false return textField }() let passwordTextField: UITextField = { let textField = UITextField() textField.placeholder = "密码" textField.borderStyle = .roundedRect textField.isSecureTextEntry = true textField.translatesAutoresizingMaskIntoConstraints = false return textField }() let registerButton: UIButton = { let button = UIButton(type: .system) button.setTitle("注册", for: .normal) button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 24) button.addTarget(self, action: #selector(registerButtonTapped), for: .touchUpInside) button.translatesAutoresizingMaskIntoConstraints = false return button }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white navigationItem.title = "注册" view.addSubview(titleLabel) view.addSubview(emailTextField) view.addSubview(passwordTextField) view.addSubview(registerButton) NSLayoutConstraint.activate([ titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), titleLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 50), emailTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor), emailTextField.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 50), emailTextField.widthAnchor.constraint(equalToConstant: 300), passwordTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor), passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 20), passwordTextField.widthAnchor.constraint(equalToConstant: 300), registerButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), registerButton.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 50), registerButton.widthAnchor.constraint(equalToConstant: 200), registerButton.heightAnchor.constraint(equalToConstant: 50) ]) } @objc func registerButtonTapped() { // 注册逻辑 } } ``` 这个注册界面包括了一个标题、一个邮箱输入框、一个密码输入框和一个注册按钮。在视图加载时,我们添加了这些视图并使用了自动布局约束来布局它们。 当用户点击注册按钮时,`registerButtonTapped` 方法将会被调用。你可以在这个方法中添加你自己的注册逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北方人深漂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值