在键盘上显示带有按钮的 UIView,如 Skype、Viber 信使 (Swift、iOS)
Show UIView with buttons over keyboard, like in Skype,Viber messengers (Swift, iOS)
我想创建附件视图,放置在输入附件视图下,通过键盘,就像在 Skype 应用程序或 Viber 中一样:
我已经问过这样的问题 ,但是这个问题的建议解决方案并不是那么优雅,因为当我将滚动视图拖到顶部时,我想用键盘将我的附件 UIView 向下移动(我使用 UIScrollViewKeyboardDismissModeInteractive)。
所以我创建了一个函数,找出视图,其中放置了键盘和我的自定义输入附件视图:
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
}
然后我添加了我的自定义 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))
}
这会在输入附件视图下和键盘上方创建自定义 UIView,当我向上滚动时它会移动。但是当我想按下按钮时,我按下键盘上的一个键。
那么如何将此视图移动到 UIInputSetHostView 中视图层次结构的顶部?
好的,多亏了 Brian Nickel,我发现了可能不是优雅但非常简单的解决方案。所以我重写了 inputAccessoryView 以在键盘上创建一个工具栏。所以基本上,如果我按下此工具栏上的附加按钮,我想看到另一个 inputView,而不是键盘。
所以在我的自定义输入附件视图 class 中,我只创建了一些隐藏的 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)
....
}
所以在我的主视图控制器中,我创建了函数,为这个新创建的 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 的输入视图。我的附件视图就在输入附件视图下。当我再次按下附加按钮时,我使用默认的 inputView 为我的主 textView 重新初始化 inputView,这是键盘视图。
我想创建附件视图,放置在输入附件视图下,通过键盘,就像在 Skype 应用程序或 Viber 中一样:
我已经问过这样的问题
所以我创建了一个函数,找出视图,其中放置了键盘和我的自定义输入附件视图:
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
}
然后我添加了我的自定义 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))
}
这会在输入附件视图下和键盘上方创建自定义 UIView,当我向上滚动时它会移动。但是当我想按下按钮时,我按下键盘上的一个键。
那么如何将此视图移动到 UIInputSetHostView 中视图层次结构的顶部?
好的,多亏了 Brian Nickel,我发现了可能不是优雅但非常简单的解决方案。所以我重写了 inputAccessoryView 以在键盘上创建一个工具栏。所以基本上,如果我按下此工具栏上的附加按钮,我想看到另一个 inputView,而不是键盘。 所以在我的自定义输入附件视图 class 中,我只创建了一些隐藏的 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)
....
}
所以在我的主视图控制器中,我创建了函数,为这个新创建的 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 的输入视图。我的附件视图就在输入附件视图下。当我再次按下附加按钮时,我使用默认的 inputView 为我的主 textView 重新初始化 inputView,这是键盘视图。