NinjaDeveloper
NinjaDeveloper

Reputation: 1712

button image is not show in rightView of UItextfield

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

enter image description here

Upvotes: 2

Views: 1564

Answers (1)

unkgd
unkgd

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

Related Questions