Show UIView with buttons on the keyboard, for example, in Skype, Viber messengers (Swift, iOS)

I want to create an attachment view, which is placed under the appearance of an auxiliary input, on top of the keyboard, for example, in a Skype or Viber application:

enter image description here

I already asked such a question here , but the proposed solution for this question was not so elegant, because when I drag my scroll view up, I want the UIView application to move down using the keyboard (I use UIScrollViewKeyboardDismissModeInteractive).

So, I create a function that detects the view where the keyboard and my custom accessory view are located:

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 } 

enter image description here

And after that I add my custom UIView and create a few restrictions:

  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)) } 

Create a custom UIView as an auxiliary input and above the keyboard, and it moves as you scroll up. But when I want to press a button, I press a key on the keyboard.

enter image description here enter image description here

So, how can I move this view to the top hierarchy of the view in UIInputSetHostView?

+6
ios uikeyboard swift uiview
source share
1 answer

Okey, thanks to Brian Nickel, I found, maybe not an elegant, but very simple solution. So I created an override of inputAccessoryView to create a toolbar above the keyboard. So basically, if I click on the attach button on this toolbar, I want to see another input view, not the keyboard. Thus, in my class input user interface, I created only 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 a function that reinitializes the inputView for this newly created attachTextView file, 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 } } 

Therefore, when I click the attach button, my attachTextView becomes the first responder, and then I reinitialize the input view for this text element. And I got the look of attached attachments right under the guise of an accessory. And when I click the attach button again, I reinitialize the inputView with the default inputView for my main text view, which is the keyboard view.

+3
source share

All Articles