InputAccessoryView 未删除

InputAccessoryView not removed

我正在制作一个 iOS 应用程序,我在其中使用 inputaccessoryviewUIViewController

问题是

控制器 A

   class  ControllerA: UIViewController { 

    override var inputAccessoryView: UIView? {
        return saveView
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }

    @IBAction func openVCB(_ sender: UIButton) {

    let controllerB = controllerB.instantiate(fromAppStoryboard: .main)
        let navigationController = UINavigationController(rootViewController: controllerB)
        navigationController.navigationBar.isTranslucent = false
        self.present(navigationController, animated: true)
    }
}

控制器 B

   class  ControllerB: UIViewController { 

    override var inputAccessoryView: UIView? {
        return nil
    }

    override var canBecomeFirstResponder: Bool {
        return false
    }

}

controller A 中,视图按预期工作,但在 controller B 中,如果我打开键盘并关闭它,AccossoryView 它将可见,但它应该不可见可见,有什么想法吗?

完整项目InpoutViewTest

我也 运行 遇到过这个问题。尝试将其放入 viewWillDisappear(_ animated: Bool) 函数中:

override func viewWillDisappear(_ animated: Bool) {
   if let myInputAccessoryView = saveView {
      myInputAccessoryView.removeFromSuperview()
   }
}

它应该在转换到 controller B 之前删除输入附件视图。只需记得在返回 controller A 时将输入附件视图添加回来。如果您愿意,也可以尝试 myInputAccessoryView.isHidden = true 而不是 removeFromSuperview()

遵循这种方法:

var shouldBecomeFirstResponder:Bool = false

override var canBecomeFirstResponder: Bool {
return shouldBecomeFirstResponder
}

并且不要忘记在 viewWillDisappear 中也将此标志设置为 false

 shouldBecomeFirstResponder = false