问题描述
我想创建附件视图,放置在输入附件视图下方,通过键盘,如Skype App或Viber:
我已经问过这样的问题
之后我添加了我的自定义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(NSL ayoutConstraint(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中的视图层次结构顶部?
Okey,感谢Brian Nickel,我发现也许不是优雅但非常简单的解决方案。所以我已经覆盖inputAccessoryView以在键盘上创建工具栏。所以基本上,如果我按下这个工具栏上的附加按钮,我想看到另一个inputView,而不是键盘。
所以在我的自定义输入附件视图类中,我创建了一些隐藏的textView:
class MessageChatInputAccessoryView:UIToolbar {
var textView:UITextView! // textView用于输入文本
var sendButton:UIButton! //发送消息
var attachButton:UIButton! //附加按钮+
var attachTextView:UITextView! - >这一个
覆盖init(帧:CGRect){
super.init(frame:frame)
.....
.....
attachTextView = UITextView(框架: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.attachTextV iew.reloadInputViews()
attachMenuIsShown = true
}
}
因此,当我按下附加按钮时,我的attachTextView成为第一响应者,而不是重新初始化此textView的输入视图。我在输入附件视图下方获得了附件视图。当我再次按下附加按钮时,我使用默认inputView为我的主textView重新初始化inputView,这是键盘视图。
I want to create attachments view, that places under input accessory view, over keyboard, like in Skype App or Viber:
I already asked such question here, but suggested solution for this question was not so elegant, because when i drag my scroll view to the top, i want to my Attachment UIView move down with keyboard (I use UIScrollViewKeyboardDismissModeInteractive).
So i create a function, that find out the view, where keyboard and my custom input accessory view are placed:
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
}
And after it i add a my custom UIView and create a few constraints:
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))
}
This create custom UIView under input accessory view and above keyboard, and it moves when i scroll up. But when i want to press on button, i press a key on keyboard.
So how can i move this view to the top of view hierarchy in UIInputSetHostView?
Okey, thanks to Brian Nickel, i found maybe not the elegant, but very simple solution. So i had override inputAccessoryView to create a toolbar over keyboard. So basically, if i press on attach button on this toolbar, i want to see another inputView, not a keyboard.So in my custom input accessory view class i created just some textView, that is hidden:
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)
....
}
So in my main view controller, i created function, the re-initialize inputView for this newly created attachTextView, something like this:
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
}
}
So when i press on attach button, my attachTextView becomes first responder, and than i re-initialize input view for this textView. And i got my attachments view right under input accessory view. And when i press attach button once again, i re-initialize inputView with default inputView for my main textView, which is keyboard view.
这篇关于用键盘上的按钮显示UIView,比如Skype,Viber messengers(Swift,iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!