我想创建附件视图,放置在输入附件视图下,通过键盘,如在 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 中的视图层次结构的顶部?