【发布时间】:2017-05-03 13:24:40
【问题描述】:
当我的文本字段成为第一响应者时,我想在我的键盘上方显示一个自定义 UIView。
但是,我似乎可以同时显示视图和键盘?
有办法克服吗?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
phoneInputTextField.becomeFirstResponder()
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil)
}
func handleKeyboardNotification(notification: NSNotification) {
guard let userInfo = notification.userInfo,
let frameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue
else { return }
let customView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: frameValue.cgRectValue.width + 20))
customView.backgroundColor = UIColor.black
phoneInputTextField.inputView = customView
}
该代码仅显示键盘。
【问题讨论】:
-
首先,
.inputView替换键盘,所以无论如何你不能同时显示两者...你真的想要.inputAccessoryView吗?跨度>