lazy var cancelBtn: UIButton = { let button = UIButton() button.translatesAutoresizingMaskIntoConstraints = false //这句一定要写 button.setTitle(NSLocalizedString("cancel", comment: ""), for: .normal) button.setTitleColor(UIColor.ne_blueText, for: .normal) button.titleLabel?.font = UIFont.systemFont(ofSize: 13) button.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside) return button }()
addSubview(cancelBtn) NSLayoutConstraint.activate([ cancelBtn.leftAnchor.constraint(equalTo: leftAnchor, constant: 15), cancelBtn.topAnchor.constraint(equalTo: topAnchor, constant: 8), cancelBtn.widthAnchor.constraint(equalToConstant: 45), ])
安全区代码
if #available(iOS 11.0, *) { NSLayoutConstraint.activate([ copyright.centerXAnchor.constraint(equalTo: view.centerXAnchor), copyright.bottomAnchor.constraint( equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -48 ), ]) }
func setupSubviews() { view.addSubview(textfieldBgView) if #available(iOS 11.0, *) { NSLayoutConstraint.activate([ textfieldBgView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0), textfieldBgView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20), textfieldBgView.topAnchor.constraint( equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 12 ), textfieldBgView.heightAnchor.constraint(equalToConstant: 50), ]) } else { if #available(iOS 10.0, *) { NSLayoutConstraint.activate([ textfieldBgView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0), textfieldBgView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20), textfieldBgView.topAnchor.constraint(equalTo: view.topAnchor, constant: 12), textfieldBgView.heightAnchor.constraint(equalToConstant: 50), ]) } else { NSLayoutConstraint.activate([ textfieldBgView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0), textfieldBgView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20), textfieldBgView.topAnchor.constraint(equalTo: view.topAnchor, constant: 12 + kNavigationHeight + KStatusBarHeight), textfieldBgView.heightAnchor.constraint(equalToConstant: 50), ]) } } textfieldBgView.addSubview(textField) NSLayoutConstraint.activate([ textField.leftAnchor.constraint(equalTo: textfieldBgView.leftAnchor, constant: 16), textField.rightAnchor.constraint(equalTo: textfieldBgView.rightAnchor, constant: -12), textField.topAnchor.constraint(equalTo: textfieldBgView.topAnchor, constant: 0), textField.heightAnchor.constraint(equalToConstant: 44), ]) }