I am building a password field with a button that allows the user to toggle the password visibility. The code makes use of a text field and a toggle button that is in the text field's right view. The padding of the button was defined using:
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
But as of Xcode 15, imageEdgeInsets
was deprecated and ignored. The warning reads:
'imageEdgeInsets' was deprecated in iOS 15.0: This property is ignored when using UIButtonConfiguration
Does anyone know what to use instead to adjust the padding?
Here is my code:
import UIKit
class ViewController: UIViewController {
let button = UIButton(type: .custom)
var passwordToggleFlag: Bool = true
@IBOutlet weak var passwordTextfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(systemName: "eye.slash"), for: .normal)
button.tintColor = .link
button.backgroundColor = UIColor.white
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.addTarget(self, action: #selector(passwordToggleButtonPressed), for: .touchUpInside)
passwordTextfield.rightView = button
passwordTextfield.rightViewMode = .always
passwordTextfield.autocorrectionType = .no
passwordTextfield.layer.borderWidth = 2
passwordTextfield.layer.cornerRadius = 5
passwordTextfield.layer.borderColor = UIColor.lightGray.cgColor
passwordTextfield.isSecureTextEntry = true
passwordToggleFlag = false
}
@objc func passwordToggleButtonPressed(_ sender:UIButton!)
{
print("Button tapped")
if(passwordToggleFlag == false){
print("false")
passwordToggleFlag = true
button.setImage(UIImage(systemName: "eye"), for: .normal)
button.tintColor = .link
passwordTextfield.isSecureTextEntry = false
}else{
print("true")
passwordToggleFlag = false
button.setImage(UIImage(systemName: "eye.slash"), for: .normal)
button.tintColor = .link
passwordTextfield.isSecureTextEntry = true
}
}
}
I have tried some different suggestions that I found online but nothing I found works. This is a very simple feature and there must be a straight forward solution.