8

There is a view with UITextView on it. UITextView is the only one possible firstResponder for keyboard. Let's call it "BackView".

There is another view which is PopUp (full screen view with transparent background which fades underlying view). Let's call it "PopUp".

PopUp works fine except when the keyboard is on screen. Presenting PopUp forces keyboard to be hidden and, when the PopUp is dismissed, the keyboard is shown again. Since the PopUp is not covering whole BackView it does not look good.

Is there any way to keep keyboard on BackView while PopUp is shown? (there is no need to use keyboard in popup).

PopUp contains:

  • full screen view with background set to black colour with alpha (to fade underlaying view)
  • several images, labels and Close button.

PopUp is shown with:

[self setModalPresentationStyle:UIModalPresentationCurrentContext];
[self presentViewController:vc animated:YES completion:nil];

Found solution:

Add PopUp view as a subview for current window

mb.sever
  • 103
  • 6
  • If I understand it correctly, you need to add 'vc' as your child view controller and then just add its view as a subview of your current controller (the one that displays the keyboard). Don't ask system to present it, just do it yourself (because you're not changing the context... at least you haven't indicated so by providing details about PopUp). Tag it so you can remove it once you fade it off. – igraczech Jul 03 '14 at 15:40
  • thx for suggestion. Tried a PopUp as subview. In this case keyboard is shown above my PopUp :( And there is no fading – mb.sever Jul 03 '14 at 15:56
  • 1
    igraczech, thx again. All work's fine if add subview to window (not current controller). – mb.sever Jul 04 '14 at 07:40
  • You can also display it inside a popover. That should cover the keyboard as well. – igraczech Jul 04 '14 at 07:41
  • igraczech, popover is actually what i'm trying to do. But as far as I understand it is available only for iPad? – mb.sever Jul 04 '14 at 07:52
  • Yeah, that's kind of true. http://stackoverflow.com/questions/14787765/uipopovercontroller-for-iphone-not-working – igraczech Jul 04 '14 at 08:11

2 Answers2

1

mb.server answered his own question (one that I had as well). But I just wanted to spell out the code explicitly for the benefit of others:

//instantiate popUpVC, then
popUpVC.view.frame = [UIScreen mainScreen].bounds; //assuming you wish the popover to occupy the entire screen; adjust as needed
UIWindow* currentWindow = [[[UIApplication sharedApplication] windows] lastObject];
[currentWindow addSubview:popUpVC.view];
T'Pol
  • 339
  • 4
  • 9
0

Show:

if let popupVC = storyboard.instantiateViewControllerWithIdentifier("PopupVC") as? PopupVC
    {
        var frame = UIScreen.mainScreen().bounds
        frame.origin.y = frame.size.height
        overlayWindow = UIWindow(frame: frame)

        popupVC.overlayWindow = overlayWindow
        overlayWindow.windowLevel = UIWindowLevelAlert
        overlayWindow.rootViewController = popupVC

        overlayWindow.makeKeyAndVisible()

        UIView.animateWithDuration(0.3, animations:
        {
            self.overlayWindow.frame.origin.y = 0
        })
    }

Hide in PopupVC

 if let window = overlayWindow
    {
        UIView.animateWithDuration(0.3, animations: {
            window.frame.origin.y = window.frame.height
        }, completion: { (finished) -> Void in
            self.overlayWindow = nil
        })
    }
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86