首页 > 解决方案 > Textfield的rightView上有两个UIButtons

问题描述

我有一个问题,现在只显示一个按钮。我想要“Vergessen”?除非用户输入内容,否则要显示的按钮。在编辑“眼睛”按钮时应该出现。现在只有其中一个被显示,我不知道为什么。有没有一种简单的方法来改变“眼睛”按钮的大小?

func createForgetButton () {
    let button = UIButton(type: .system)
    button.setTitle("Vergessen?", for: .normal)
    button.addTarget(self, action: #selector(self.vergessenTapped(_:)), for: .touchUpInside)
    button.titleLabel?.font = UIFont(name: "Avenir Next", size: 19.0)
    passwordTextField.rightView = button
    passwordTextField.rightViewMode = .unlessEditing

}

func createTogglePassword () {
    let button = UIButton(type: .system)
    button.setBackgroundImage(UIImage(named: "eyeOpen"), for: .normal)
    button.addTarget(self, action: #selector(togglePassword(_:)), for: .touchUpInside)
    passwordTextField.rightView = button
    passwordTextField.rightViewMode = .whileEditing
}

//我的文本字段的子类:

override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
    let offset = -20
    let width  = 125
    let height = 125
    let x = Int(bounds.width) - width - offset
    let y = offset
    let rightViewBounds = CGRect(x: x, y: y, width: width, height: height)
    return rightViewBounds
}

在此处输入图像描述

标签: iosswiftuibuttonuitextfield

解决方案


推荐阅读