我已将 UIButton 放置到文本字段的右视图中。但我看到下面的警告
我的 UIButton 代码:
private lazy var myButton : UIButton = {
let button = UIButton()
var config = UIButton.Configuration.plain()
config.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 16)
button.configuration = config
button.frame = CGRect(x: CGFloat(myTextfield.frame.size.width - 16), y:(myTextfield.frame.size.height - 16) / 2, width: CGFloat(40), height: CGFloat(30))
button.contentMode = .scaleAspectFit
button.backgroundColor = .clear
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(passwordButtonImage, for: .normal)
button.addTarget(self, action: #selector(self.toggle(_:)), for: .touchUpInside)
return button
}()
将其添加到文本字段的代码:
@IBOutlet private weak var mytextField: UITextField {
didSet {
mytextField.rightView = myButton
mytextField.rightViewMode = .always
}
}
您需要添加约束来水平和垂直放置按钮并调整其大小。在自动布局中框架是没有用的。唯一重要的是你提供的约束。没有它们,您的按钮会漂浮在周围,而不会告诉系统将其放置在哪里。
或者,您可以保留
translatesAutoresizingMaskIntoConstraints
true 并添加“支柱和弹簧”自动调整大小蒙版。