4

In iOS 7, I show actionSheet by "showFromRect":

[actionSheet showFromRect:rect inView:view animated:YES];

But in iOS 8, this doesn't work. They they replace the implementation and suggest us using UIAlertController. Then how do I show this actionSheet like a popover?

allenlinli
  • 2,066
  • 3
  • 27
  • 49
  • I tried using UIPopoverController, with UIAlertController in it, still doesnt work. UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:alertController]; – allenlinli Aug 07 '14 at 03:40

4 Answers4

12

Using UIAlertController you can access the popoverPresentationController property to set the sourceView (aka inView) and sourceRect (aka fromRect). This gives the same appearance as the previous showFromRect:inView:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];

// Set the sourceView.
alert.popoverPresentationController.sourceView = self.mySubView;

// Set the sourceRect.
alert.popoverPresentationController.sourceRect = CGRectMake(50, 50, 10, 10);

// Create and add an Action.
UIAlertAction *anAction = [UIAlertAction actionWithTitle:@"Action Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"Action Pressed");
}];

[alert addAction:anAction];

// Show the Alert.
[self presentViewController:alert animated:YES completion:nil];
tagy22
  • 1,353
  • 17
  • 26
3

I also meet this problem like you, but my case is that I show a UIActionSheet in UIWindow.view on the iPad device, and the UIWindow doesn't set rootViewController.

So, I found, if we show a UIActionSheet in a window whose rootViewController equals to nil, the UIActionSheet could not show out.

My solution is to set

window.rootViewController = [[UIViewController alloc] init];

then,

[actionSheet showFromRect:rect inView:window.rootViewController.view animated:YES].

Hope this will help you!

Pang
  • 9,564
  • 146
  • 81
  • 122
jackie
  • 31
  • 3
-1

I found out the point is in iOS 7, you show actionSheet in a new window actually when using "showFromRect: inView:" ;

But in iOS 8 you show the actionSheet just on the view you send in parameters.

So the solution is to send

self.superView
    [actionSheet showFromRect:rect inView:[self.superView] animated:YES];

Me and my colleague figured it out by randomly trying.

User_1191
  • 981
  • 2
  • 8
  • 24
allenlinli
  • 2,066
  • 3
  • 27
  • 49
  • unfortunately it doesn't work in popovers: `Presenting view controllers on detached view controllers is discouraged . ` – Nikita Took Nov 20 '14 at 08:02
-1

according to this https://developer.apple.com/library/ios/documentation/Uikit/reference/UIActionSheet_Class/index.html thread UIActionSheet is deprecated, you should use UIAlertController instead of UIActionSheet.

Thanks.

Banker Mittal
  • 1,918
  • 14
  • 26