0

通知发生了一种奇怪的行为。每当我切换UITextfield到另一个时,我的通知都会正常触发。但最近我注意到它们在从一个切换UITextField到另一个时不会被触发,而是在键盘隐藏时正确触发。苹果改变这个逻辑吗?

我需要将字段滚动到可见,并且由于观察者在切换时现在没有触发,因此字段不会变为可见矩形。

我通过在 上发布通知创建了一个替代方案textFieldDidBeginEditing,但如果可能的话,我想回到旧的方式。

4

2 回答 2

1

我找到了问题的真正原因。UITextfield如果没有附件视图,则在切换时不会调用键盘通知,并且在附件视图的情况下,每次都会调用它们(为什么每次选择另一个 TextField 时都会调用 UIKeyboardWillShowNotification?

在我的情况下,我在每个字段上都有附件视图,但是在添加附件视图时,我向所有字段添加了相同的视图,这是真正的问题。我需要将单独的 UIView 实例分配给不同的字段。一旦我这样做了,我的问题就消失了。

于 2018-10-05T07:45:57.223 回答
0

我遇到了完全相同的问题,我通过创建一个带有新按钮的附件视图来处理它,该按钮是我实际按钮的副本。所以基本上,考虑你有一个UIButton粘在底部的UIViewController,你有 2 个UITextField,当互换切换时,感觉不到它UIButton曾经移动到其他地方,但仍然卡在键盘上。下面这段代码解释了如何解决这个问题:

@IBOutlet weak var signInBtn: UIButton!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!

var accessoryViewKeyboard:UIView?
var btnAccessory:UIButton?


override func viewDidLoad() {
    super.viewDidLoad()

    passwordTextField.delegate = self
    emailTextField.delegate = self

    accessoryViewKeyboard = UIView(frame: signInBtn.frame)

    //Inputting the "accessoryViewKeyboard" here as the "inputAccessoryView" is of 
    //utmost importance to help the "signInBtn" to show up on tap of different "UITextFields"
    emailTextField.inputAccessoryView = accessoryViewKeyboard
    passwordTextField.inputAccessoryView = accessoryViewKeyboard

    setupBtnWithKeyboard()
}

func setupBtnWithKeyboard() {
    btnAccessory = UIButton(frame: CGRect(x: signInBtn.frame.origin.x, y: signInBtn.frame.origin.y, width: self.view.frame.size.width, height: signInBtn.frame.size.height))
    accessoryViewKeyboard?.addSubview(btnAccessory!)
    btnAccessory?.translatesAutoresizingMaskIntoConstraints = false
    btnAccessory?.frame = CGRect(x: (accessoryViewKeyboard?.frame.origin.x)!,
                                 y: (accessoryViewKeyboard?.frame.origin.y)!,
                                 width: self.view.frame.size.width,
                                 height: (accessoryViewKeyboard?.frame.size.height)!)

    btnAccessory?.backgroundColor = UIColor(red: 31/255, green: 33/255, blue: 108/255, alpha: 1)
    btnAccessory?.setTitle("Sign In", for: .normal)
    btnAccessory?.titleLabel?.font = .systemFont(ofSize: 22)
    btnAccessory?.titleLabel?.textColor = UIColor.white
    btnAccessory?.titleLabel?.textAlignment = .center
    btnAccessory?.isEnabled = true
    btnAccessory?.addTarget(self, action: #selector(SignIn.signInBtnPressed), for: .touchUpInside)

    NSLayoutConstraint.activate([
        btnAccessory!.leadingAnchor.constraint(equalTo:
            accessoryViewKeyboard!.leadingAnchor, constant: 0),
        btnAccessory!.centerYAnchor.constraint(equalTo:
            accessoryViewKeyboard!.centerYAnchor),
        btnAccessory!.trailingAnchor.constraint(equalTo:
            accessoryViewKeyboard!.trailingAnchor, constant: 0),
        btnAccessory!.heightAnchor.constraint(equalToConstant: signInBtn.frame.size.height),
        ])
}

你完成了。这将使UIButton键盘上始终存在。重要的是无论UITextField您介绍多少个实例,始终输入accessoryViewKeyboard作为它的inputAccessoryView.

于 2019-03-02T18:58:36.327 回答