iOS-切换UITextfields时不会触发键盘通知

时间:2018-10-04 12:49:35

标签: ios nsnotifications xcode10 ios12 swift4.2

通知正在发生一种奇怪的行为。每当我将一个UITextfield切换到另一个UITextField时,通知都会正确触发。但是最近我注意到,从一个textFieldDidBeginEditing切换到另一个text-decoration:none; 时,它们并没有被触发,但是当键盘被隐藏时,它们却可以正常触发。 Apple 是否更改了此逻辑?

我需要将字段滚动到可见状态,并且由于观察者现在在切换时未触发,因此这些字段不可见。

我已经通过在newArray = []; for (var i = 0; i < gridsize; i ++) { for (var j = 0; j < gridsize; j ++) { newArray.push(grid[i][j]) } } newArray = shuffle(newArray) 上发布通知来创建替代方法,但是如果可能的话,我想返回旧的方法。

2 个答案:

答案 0 :(得分:0)

我找到了问题的真正原因。如果UITextfield没有附件视图,则切换时不会调用键盘通知;在附件视图的情况下,每次(Why is UIKeyboardWillShowNotification called every time another TextField is selected?)都会调用键盘通知

就我而言,我在每个字段上都有附件视图,但是在添加附件视图时,我在所有字段中都添加了相同的视图,这才是真正的问题。我需要将UIView的单独实例分配给不同的字段。一经发现我的问题就解决了。

答案 1 :(得分:0)

我遇到了完全相同的问题,我通过使用新按钮创建了一个attachmentView来解决了这个问题,该按钮是我实际按钮的重复。因此,基本上,假设您有一个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