7

I present UIAlertController action sheet from my UIViewController:

UIAlertController sheet = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:sheet animated:YES completion:nil];

This works fine for the most part. Except when I have keyboard present, the sheet appears behind the keyboard. Is this expected behaviour? Is there another parameter I need to specify or present it from somewhere else?

anna
  • 2,723
  • 4
  • 28
  • 37
  • Possible duplicate of [KEEP keyboard ON when UIAlertcontroller is presented in Swift?](http://stackoverflow.com/questions/28564710/keep-keyboard-on-when-uialertcontroller-is-presented-in-swift) – gblazex Feb 24 '16 at 14:41

2 Answers2

1

It appears to be expected behaviour since the documentation says that alert controller with UIAlertControllerStyleActionSheet is presented within current context. Considering that the keyboard resides in another window, it does not share context with the presenting view controller.

For the purposes of my project, I decided to fall back on the old UIActionSheet, which presents over the keyboard no problem. However, if there are suggestions for UIAlertController, feel free to post as well.

anna
  • 2,723
  • 4
  • 28
  • 37
1

Issue is not reproducible anymore. Probably it was fixed by  or requires special hierarchy of view controllers. If we assume that this issue can still be reproduced in some circumstances I can suggest :

  1. Hide keyboard when presenting UIAlertController by calling UIView.endEditing(true) where view is equal to UIViewConroller.view or editable view (e.g. UITextView)

  2. Show keyboard after dismissing UIAlertController if keyboard was presented before by calling UIView.becomeFirstResponder() where view is view edited on first step

We can try do these steps in more general way, like creating an extension or subclassing UIAlertController. But I believe we should not over complicate things

Silmaril
  • 4,241
  • 20
  • 22
  • Last minute bounty panic? :-) Could you please elaborate in more detail on the HOW? How to hide the keyboard, and even more interesting how to show keyboard with the same first responder as before? For the bounty you will have to show some code + argue why you solution is the most appropriate one. – fabb Jan 09 '15 at 10:25
  • 1. It's not difficult to hide keyboard: [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; 2. And finding current first responder: http://stackoverflow.com/a/1823360/3362694 But, I think it's not elegant solution. – perpetuous Jan 09 '15 at 10:56
  • It is not bounty panic :) I just saw this question today. Sure I'll provide some code – Silmaril Jan 09 '15 at 14:06
  • fabb, are you sure it is still an issue? I can't reproduce it. See sample project: https://dl.dropboxusercontent.com/u/38817855/alert.zip – Silmaril Jan 09 '15 at 15:49
  • You are completely right. I even tried in Xcode 6.0.1 with simulator 8.0, and it also works there correctly. No idea why it was not working for me in my project... Adapt your answer and get some points. – fabb Jan 10 '15 at 13:17