Apple has deprecated the action sheet in iOS 8.3 how do you add an action sheet to my user interface?
I realized Apple's documentation isn't very clear on how to create an action sheet with UIAlertController. So after a little playing around I just wanted to share my code, since I couldn't really find anything useful on Stack Exchange about this topic.
Here is a snippet of the code I used in my apps for an action sheet, just in case anyone needed help trying to figure out how to use UIAlertController as an Action Sheet.
I had the same problem in my iPhone app, with an
UIActionSheet
to ask the user if they wanted to take a photo, or pick an image from their gallery.Prior to iOS 9, the following code worked fine:
However, in iOS 9, all this does is completely darken the screen, and nothing would appear.
Yeah... thanks Apple.
The solution is to replace the
UIActionSheet
with aUIAlertController
.Notice that if you don't include a cancel option (an option with the
UIAlertActionStyleCancel
style), then the action sheet will appear, but tapping anywhere outside of the action sheet won't dismiss the menu.One gotcha:
Even though we've specified that we want this
UIAlertController
to have the styleUIAlertControllerStyleActionSheet
, you need to set the title asnil
, rather than a blank string, otherwise you get an ugly gap at the top of the window.I can't wait to see what perfectly-working code iOS 9.2 will break...
Update
My comment: "However, in iOS 9, all this does is completely darken the screen, and nothing would appear" was slightly wrong.
Actually, I was opening my
UIActionSheet
while the onscreen keyboard was visible. And in iOS 9, the keyboard will appear on top of yourUIActionSheet
, so you can no longer see your action sheet (!!), but you do see that the rest of the screen has turned darker.With
UIAlertController
, iOS is slightly more user-friendly, as it hides the onscreen keyboard before attempting to display the action sheet at the bottom of the screen. Exactly why Apple doesn't do the same withUIActionSheets
is beyond me.(Sigh.)
Please may I go back to using Visual Studio, now..?
Another update
Apple has said that UIActionSheets are now "deprecated in iOS 8".
However, if you want to keep using them, on your iOS screens which contain textboxes, then a workaround to this bug, errr, problem, is to add one line of code before displaying your
UIActionSheet
:This makes sure the onscreen keyboard is dismissed before displaying your "deprecated" action sheet..
(Sigh.) If anyone needs me, I'll be in the pub.
In iOS 9 and for iPad I had to add some important changes. May be someone in future might need this.