gpt4 book ai didi

ios - 当 textField 成为第一响应者时,我可以同时显示键盘和输入 View 吗

转载 作者:行者123 更新时间:2023-11-28 21:10:17 25 4
gpt4 key购买 nike

当我的文本字段成为第一响应者时,我想在我的键盘上方显示一个自定义 UIView。

但是,我似乎可以同时显示 View 和键盘?

有没有办法克服它?

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。

  • inputView 用于分配一些自定义 View 来替换 UIKeyboard ,就像您可以在编辑文本字段时使用 UIPickerView 、 UIDatePicker 等

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

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

示例:

yourTxtField.inputAccessoryView = yourCustomView()

关于ios - 当 textField 成为第一响应者时,我可以同时显示键盘和输入 View 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com