gpt4 book ai didi

ios - 在键盘上显示带有按钮的 UIView,例如 Skype、Viber 信使(Swift、iOS)

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:17 25 4
gpt4 key购买 nike

我想创建附件 View ,放置在输入附件 View 下,通过键盘,就像在 Skype 应用程序或 Viber 中:

enter image description here

我已经问过这样的问题here ,但是针对这个问题的建议解决方案并不是那么优雅,因为当我将 ScrollView 拖到顶部时,我希望我的附件 UIView 使用键盘向下移动(我使用 UIScrollViewKeyboardDismissModeInteractive)。

所以我创建了一个函数,找出 View ,其中放置了键盘和我的自定义输入附件 View :

    func findKeyboardView() -> UIView? {
var result: UIView? = nil

let windows = UIApplication.sharedApplication().windows
for window in windows {
if window.description.hasPrefix("<UITextEffectsWindow") {
for subview in window.subviews {
if subview.description.hasPrefix("<UIInputSetContainerView") {
for sv in subview.subviews {
if sv.description.hasPrefix("<UIInputSetHostView") {
result = sv as? UIView
break
}
}
break
}
}
break
}
}
return result
}

enter image description here

然后我添加了我的自定义 UIView 并创建了一些约束:

    func createAttachView() {
attach = MessageChatAttachmentsView(frame: CGRectZero)
let newView = findKeyboardView()
newView!.addSubview(attach!)
newView!.addConstraint(NSLayoutConstraint(item: accessoryView, attribute: .Bottom, relatedBy: .Equal, toItem: attach!, attribute: .Top, multiplier: 1.0, constant: 0.0))
attach!.addConstraint(NSLayoutConstraint(item: attach!, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 260))
attach!.addConstraint(NSLayoutConstraint(item: attach!, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 320))
}

这会在输入附件 View 下和键盘上方创建自定义 UIView,当我向上滚动时它会移动。但是当我想按下按钮时,我按下键盘上的一个键。

enter image description here enter image description here

那么如何将此 View 移动到 UIInputSetHostView 中 View 层次结构的顶部?

最佳答案

好的,多亏了 Brian Nickel,我找到了可能不是优雅但非常简单的解决方案。所以我重写了 inputAccessoryView 以在键盘上创建一个工具栏。所以基本上,如果我按下此工具栏上的附加按钮,我想看到另一个 inputView,而不是键盘。所以在我的自定义输入附件 View 类中,我只创建了一些隐藏的 textView:

class MessageChatInputAccessoryView : UIToolbar {
var textView:UITextView! //textView for entering text
var sendButton: UIButton! //send message
var attachButton: UIButton! // attach button "+"
var attachTextView:UITextView! --> this one

override init(frame: CGRect) {
super.init(frame: frame)
.....
.....
attachTextView = UITextView(frame: CGRectZero)
attachTextView.alpha = 0.0
self.addSubview(attachTextView)
....
}

所以在我的主视图 Controller 中,我创建了函数,为这个新创建的 attachTextView 重新初始化 inputView,如下所示:

func attach(sender: UIButton) {
if attachMenuIsShown {
accessoryView.attachTextView.inputView = accessoryView.textView.inputView
accessoryView.attachTextView.reloadInputViews()
attachMenuIsShown = false
} else {
accessoryView.attachTextView.becomeFirstResponder()
accessoryView.attachTextView.inputView = MessageChatAttachmentsView(frame: CGRectZero)
accessoryView.attachTextView.reloadInputViews()
attachMenuIsShown = true
}

}

因此,当我按下附加按钮时,我的 attachTextView 成为第一响应者,然后我重新初始化此 textView 的输入 View 。我的附件 View 就在输入附件 View 下。当我再次按下附加按钮时,我使用默认 inputView 为我的主 textView(键盘 View )重新初始化 inputView。

关于ios - 在键盘上显示带有按钮的 UIView,例如 Skype、Viber 信使(Swift、iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598490/

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