pav*_*l_s 6 uiview uikeyboard ios uiview-hierarchy swift
我想创建附件视图,放置在输入附件视图下,通过键盘,如Skype App或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
}
Run Code Online (Sandbox Code Playgroud)
之后,我添加了我的自定义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))
}
Run Code Online (Sandbox Code Playgroud)
这会在输入附件视图和键盘上方创建自定义UIView,当我向上滚动时它会移动.但是当我想按下按钮时,我按下键盘上的一个键.
那么如何将此视图移动到UIInputSetHostView中的视图层次结构顶部?
好吧,感谢 Brian Nickel,我找到了也许不是优雅但非常简单的解决方案。所以我重写了 inputAccessoryView 以通过键盘创建工具栏。所以基本上,如果我按下此工具栏上的附加按钮,我想看到另一个 inputView,而不是键盘。因此,在我的自定义输入附件视图类中,我只创建了一些隐藏的文本视图:
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)
....
}
Run Code Online (Sandbox Code Playgroud)
所以在我的主视图控制器中,我创建了函数,为这个新创建的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
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我按下附加按钮时,我的 AttachTextView 成为第一响应者,然后我重新初始化此 textView 的输入视图。我的附件视图就在输入附件视图下。当我再次按下附加按钮时,我使用主文本视图(即键盘视图)的默认 inputView 重新初始化 inputView 。
归档时间: |
|
查看次数: |
2177 次 |
最近记录: |