代码之家  ›  专栏  ›  技术社区  ›  Hassy

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

  •  0
  • Hassy  · 技术社区  · 7 年前

    有一种奇怪的行为正在发生 通知 . 每当我换一个时,我的通知都正常发出 UITextfield UITextField 改变了这个逻辑?

    我需要将字段滚动到可见和 在切换时没有触发,场不会显示为矩形。

    textFieldDidBeginEditing ,但如果可能的话,我想回到过去。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Hassy    7 年前

    我找到了问题的真正原因。如果 UITextfield 没有附件视图,如果是附件视图,每次都会调用它们( Why is UIKeyboardWillShowNotification called every time another TextField is selected?

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

        2
  •  0
  •   Mohsin Khubaib Ahmed    6 年前

    UIButton 它粘在 UIViewController 你有两个 UITextField 当互换开关时,不能感觉 曾被移到其他地方,但仍被卡在键盘上。下面的代码解释了如何解决这个问题:

    @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),
            ])
    }
    

    你就完了。这将保持 UI按钮 总是出现在键盘上。重要的是不管有多少个 你介绍,总是输入 accessoryViewKeyboard inputAccessoryView .

    推荐文章