按钮图像未显示在 UItextfield 的 rightView 中

button image is not show in rightView of UItextfield

我使用 Sa wift 创建了一个按钮图像并将其放置在 UITextField "password" 的 rightView 上。我想在密码字段中创建切换按钮 hide/show 安全文本。右视图显示的图像。

代码:

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, 0