键盘隐藏时不要隐藏输入附件视图

问题描述

我希望 inputAccessoryView 在键盘关闭时不隐藏。我尝试在 keybaord 隐藏时更改框架,但它不起作用

     customView = UIView(frame: CGRect(x: 0,y: 0,width: 10,height: 88))
     customView.backgroundColor = UIColor.white
     textview.inputAccessoryView = customView



    // Tracking the keyboard status
     NotificationCenter.default.addobserver(self,selector: #selector(self.keyboardWillBeHidden),name: UIResponder.keyboardWillHideNotification,object: nil)


    @objc func keyboardWillHide(sender: NSNotification) {        

        self.customView.frame = CGRect(x: 0,y: self.view.frame.size.height-88,height: 88)
        
        
    }

解决方法

您需要将该视图添加到当前视图中。通过将其设置为 inputAccessoryView,您基本上是将其添加到第一响应者的视图中,在本例中为键盘。

试试这个 -

@objc func keyboardWillHide(sender: NSNotification) {        

    self.customView.frame = CGRect(x: 0,y: self.view.frame.size.height-88,width: 10,height: 88)
    
    self.view.addSubView(self.customView)
}