Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to present UIAlertController action sheet while keyboard is active?

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?

like image 514
anna Avatar asked Nov 06 '14 22:11

anna


2 Answers

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.

like image 58
anna Avatar answered Oct 06 '22 03:10

anna


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

like image 27
Silmaril Avatar answered Oct 06 '22 05:10

Silmaril