InputAccessoryView未删除

时间:2018-10-22 15:08:03

标签: xcode keyboard inputaccessoryview swift4.2

我正在制作一个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

2 个答案:

答案 0 :(得分:2)

遵循这种方法:

var shouldBecomeFirstResponder:Bool = false

override var canBecomeFirstResponder: Bool {
return shouldBecomeFirstResponder
}

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

 shouldBecomeFirstResponder = false

答案 1 :(得分:0)

我也遇到了这个问题。尝试将其放在您的viewWillDisappear(_ animated: Bool)函数中:

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

在过渡到controller B之前,应删除输入的附件视图。只要记得返回controller A时将输入的附件视图添加回去即可。如果愿意,您也可以尝试使用myInputAccessoryView.isHidden = true代替removeFromSuperview()