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.
来源:https://stackoverflow.com/questions/32598490/show-uiview-with-buttons-over-keyboard-like-in-skype-viber-messengers-swift-i