【问题标题】:Can I show both keyboard and input view while textField became first responder当 textField 成为第一响应者时,我可以同时显示键盘和输入视图吗
【发布时间】: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吗?跨度>

标签: ios keyboard textfield


【解决方案1】:

您可以同时使用 inputView 和 inputAccessoryView。

  • inputView 用于分配一些自定义视图来代替 UIKeyboard ,就像您可以在编辑 textField 时使用 UIPickerView 、 UIDatePicker 等

  • inputAccessoryView 也用于分配一些自定义视图,但不替换 UIKeyboard,它呈现在键盘上方。就像您可以根据需要使用键盘上方的 UIToolbar 和许多其他视图一样。

在您的情况下,您可以使用 inputAccessoryView

示例:

yourTxtField.inputAccessoryView = yourCustomView()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多