gpt4 book ai didi

iOS 如何使 inputaccessoryview 中的 TextView 成为第一响应者?

转载 作者:行者123 更新时间:2023-11-28 07:46:41 25 4
gpt4 key购买 nike

我想要附件 View 中的 TextView ,但只有当我在 ScrollView 中按下按钮时,附件 View 才应该可见,并且键盘应该与滑动时的 TextView 交互关闭。解决方案是什么?

Facebook Messenger 和 WhatsApp 做同样的事情,但即使键盘关闭,它们的附件 View 也可见,但我不想要它。

我的解决方案:我使附件 View 透明, TextView 是 ScrollView 的 subview ,我将 TextView 放在透明附件 View 的正上方,所以它看起来像附件 View 。但是当我这样做时, TextView 是不可触摸的,因为附件 View 窗口在它上面。

我的代码:

class ViewController: UIViewController, CustomKeyboardProtocol {

let textView = UITextView()
let button = UIButton()
let scrollView = UIScrollView()

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .black

scrollView.contentSize = CGSize(width: 10000, height: 10000)
scrollView.keyboardDismissMode = .interactive
scrollView.backgroundColor = .blue
self.view.addSubview(scrollView)

button.backgroundColor = .green
button.frame = CGRect(x: 20, y: 20, width: 30, height: 30)
button.addTarget(self, action: #selector(keyboard), for: .touchUpInside)
scrollView.addSubview(button)

textView.backgroundColor = .yellow
textView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40)
self.view.addSubview(textView)
textView.isHidden = true

let accessoryView = CustomKeyboardAccessoryView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
accessoryView.keyboardDelegate = self
accessoryView.backgroundColor = .clear
textView.inputAccessoryView = accessoryView
}

func keyboardFrameChanged(frame: CGRect) {
textView.frame = CGRect(x: 0, y: frame.origin.y, width: UIScreen.main.bounds.size.width, height: 40)
}

@objc func keyboardWillShow(notification: Notification) {
if let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardFrame.size.height
print("keyboard height: \(keyboardHeight)")
textView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - keyboardHeight, width: UIScreen.main.bounds.size.width, height: 40)
}
}

@objc func keyboardWillHide() {
textView.isHidden = true
textView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0)
}

@objc func keyboard() {
if textView.isFirstResponder {
textView.resignFirstResponder()
textView.isHidden = true
} else {
textView.becomeFirstResponder()
textView.isHidden = false
}
}

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
scrollView.frame = UIScreen.main.bounds
}

override var canBecomeFirstResponder: Bool {
return true
}
}

protocol CustomKeyboardProtocol : NSObjectProtocol {
func keyboardFrameChanged(frame : CGRect)
}

class CustomKeyboardAccessoryView: UIView {



weak var keyboardDelegate: CustomKeyboardProtocol? = nil

override func willMove(toSuperview newSuperview: UIView?) {
if newSuperview == nil {
self.superview?.removeObserver(self, forKeyPath: "center")
}
else{
newSuperview?.addObserver(self, forKeyPath: "center", options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.initial], context: nil)
}
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let theChange = change as [NSKeyValueChangeKey : AnyObject]? {
if theChange[NSKeyValueChangeKey.newKey] != nil {
if self.keyboardDelegate != nil && self.superview?.frame != nil {
self.keyboardDelegate?.keyboardFrameChanged(frame: (self.superview?.frame)!)
}
}
}


}
}

最佳答案

一种解决方法是使用 textView 属性来切换键盘,但将其隐藏在这样的键盘方法中

  @objc func keyboard() {
if textView.isFirstResponder {
textView.resignFirstResponder()
textView.isHidden = true
} else {
textView.becomeFirstResponder()
textView.isHidden = true
}
}

并创建单独的 View customView并将其添加到textView(上面提到的textView)的inputAccessoryView中

 textView.inputAccessoryView = customViewTV

在 customViewTV 中添加单独的 textView 作为 subview 并使用它

   func createCustomTV(text: String) {
customViewTV = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44))
customViewTV.backgroundColor = UIColor.red
let acceTextView = UITextView()
acceTextView.text = text
acceTextView.frame = CGRect(x: 0, y: 0, width: customViewTV.frame.width, height: customViewTV.frame.height)
acceTextView.center = customViewTV.center
acceTextView.textColor = .black
customViewTV.addSubview(acceTextView)
}

结果

enter image description here

关于iOS 如何使 inputaccessoryview 中的 TextView 成为第一响应者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50795488/

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