我正在制作一个iOS应用,其中我使用inputaccessoryview
到UIViewController
,
问题是
控制器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
答案 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()
。