UITextfield 弹跳文本

UITextfield bouncing text

我为文本字段设置了 inset

class PrimaryTextField: UITextField {

    var padding = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)

    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

}

它运行良好,但当用户在文本字段之间跳转时,文本会跳动。

问题只出现在真机上,模拟器没问题。你没有任何想法,如何解决这个问题?

您可以创建IBInspectable UITextField

class PaddedTextField: UITextField {

    @IBInspectable var padding: CGFloat = 0

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
    }
}

它也在 iOS 设备中为我工作。

你能不能试试让我知道。

keyboardWillHide 和显示通知有问题。我在那里设置动画约束。我在动画和文本不再弹跳之前在下面添加代码。

    UIView.performWithoutAnimation({
        emailTextField.layoutIfNeeded()
        passwordTextField.layoutIfNeeded()
    })