Reputation: 1712
I have created a button image and placed it on the rightView of a UITextField "password" using Sa wift. I want to create toggle button hide/show secure text in the password field. The image shown on the right view.
Code:
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
}
image
Upvotes: 2
Views: 1564
Reputation: 671
Since you are putting it inside “rightView”.
The X and Y coordinates of the frame become relative to it.
So you should change them to 0, 0
Upvotes: 4