UITextField Как включить безопасную запись текста (скрыть пароль) в Swift5 и Xcode 11 - PullRequest
0 голосов
/ 09 октября 2019

Я пытаюсь добавить пользовательскую кнопку в UITextfild. Изображение на кнопке не будет scaleToFit. С Xcode10 / Swift 4.x Код работает нормально.

Что я могу сделать? Screenshot

Фрагмент кода

import UIKit

class UIShowHideTextField: UITextField {

let rightButton  = UIButton(type: .custom)

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}

required override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

func commonInit() {
    rightButton.frame = CGRect(x:0, y:self.frame.size.width-30, width:30, height:18)
    rightButton.setImage(UIImage(named: "eyeOpen") , for: .normal)
    rightButton.contentVerticalAlignment = .fill
    rightButton.contentHorizontalAlignment = .fill
    rightButton.imageEdgeInsets = UIEdgeInsets(top: 5, left: (bounds.width - 25), bottom: 5, right: 5)
    rightButton.imageView?.contentMode = .scaleAspectFit
    rightButton.addTarget(self, action: #selector(toggleShowHide), for: .touchUpInside)




    rightViewMode = .always
    rightView = rightButton
    isSecureTextEntry = true
}

@objc
func toggleShowHide(button: UIButton) {
    toggle()
}

func toggle() {
    isSecureTextEntry = !isSecureTextEntry
    if isSecureTextEntry {
        rightButton.setImage(UIImage(named: "eyeOpen") , for: .normal)
    } else {
        rightButton.setImage(UIImage(named: "eyeClosed") , for: .normal)
    }
}

}

...