【发布时间】:2019-09-11 23:38:42
【问题描述】:
我创建了一个按钮图像,并使用 Sa wift 将其放置在 UITextField“密码”的 rightView 上。我想在密码字段中创建切换按钮隐藏/显示安全文本。右视图中显示的图像。
代码:
func passwordToggleButton(){
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "hidePassword"), for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.frame = CGRect(x: CGFloat(txtfPassword.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.backgroundColor = UIColor.white
button.addTarget(self, action: #selector(self.togglePassword), for: .touchUpInside)
txtfPassword.rightView = button
txtfPassword.rightViewMode = .always
}
图片
【问题讨论】:
-
因为你把它放在“rightView”里面,所以按钮的x和y应该是0,因为它已经相对于右视图了。
-
已修复,谢谢
-
你能把你的评论转换成答案吗
-
添加为答案,祝你好运:)
标签: ios swift button uitextfield