Accessory view on UITextField

Viewed 45

I created a textfield and a button for the eye. The privacy eye is superimposed on the password when it reaches more than 35 characters. Do you have any idea how to solve this problem? here is my code for the button

enter image description here

public var pincodeVisibilityButton: UIButton = {
    let button = UIButton()
    button.setTitleColor(.SEBlack, for: .normal)
    button.titleLabel?.font = UIFont.icon(ofSize: 34)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.isUserInteractionEnabled = true
    return button
}()
1 Answers

It is best to use built in functionality.

Reference: https://developer.apple.com/documentation/uikit/uitextfield

Sample code is below:

let overlayButton = UIButton(type: .custom)
let bookmarkImage = UIImage(systemName: "bookmark")
overlayButton.setImage(bookmarkImage, for: .normal)
overlayButton.addTarget(self, action: #selector(displayBookmarks), 
    for: .touchUpInside)
overlayButton.sizeToFit()
        
// Assign the overlay button to the text field
textField.rightView = overlayButton
textField.rightViewMode = .always

Refer:

Related