22

To show a UIActionSheet on the iPad I did this:

[actionSheetX showFromRect:RectX inView:myView animated:YES];

The arrow of the popover points down, can I change this position to point left, up or right, like when using a normal popover?

lbadias
  • 221
  • 2
  • 4

3 Answers3

25

Apple doesn't provide any access to internal structure for UIActionSheet's internal implementation of showFromRect, but as for arrow direction, there is actually a very hack-y way to work around this and sort of being able to change arrow direction to a desired direction.

Trick is to mess around the rect parameter in - (void)showFromRect:(CGRect)rect inView (UIView *)view animated:(BOOL)animated. Apple doesn't document this rect parameter very well, but if your original rect will give you a down arrow direction, feed in a rect with a large height and negative number origin.y will kind of push the action sheet popover all the way up thus showing a upward arrow direction. This is a extreme hack but it works consistently across firmware. Hope it helps.

overboming
  • 1,502
  • 1
  • 18
  • 36
  • 2
    I feel kinda guilty with the hackyness of this, but it works. – lavoy May 02 '12 at 21:02
  • 1
    I don't think this is hacky at all. Why do you think Apple provide a method signature with a separate `rect` parameter? If all you are expected to do is provide the bounds rectangle for `view` then the `rect` parameter might as well not be there. –  Sep 18 '12 at 15:50
  • I've tried to do this with a BarButtonItem and have had no luck. I tried obtaining the rectangle position from here: http://stackoverflow.com/a/5914649/590767 – fatuhoku Apr 17 '14 at 23:14
  • Okay, I managed to do it from a UIBarButtonItem on a modal. Be sure to use the presenting view controller's view rect. UIView *parentView = self.presentingViewController.view; CGRect rect = [self.navigationItem.rightBarButtonItem frameInView:parentView]; CGRect hacks = CGRectMake(rect.origin.x, -1, rect.size.width, rect.origin.y + rect.size.height); [actionSheet showFromRect:hacks inView:parentView animated:YES]; works fine for me. – fatuhoku Apr 17 '14 at 23:21
4
CGRect cellRect = cell.bounds;
cellRect.size.width = cell.frame.size.width * 2;
cellRect.origin.x = -(cell.frame.size.width + 10.0);
[_actionSheet showFromRect:cellRect inView:cell animated:YES];

Thanks to - overboming.

iPad - Split view controller: It displays action sheet with left arrow at end of the master view (table view) cell. Arrow inside with 10 pixels in the cell. Try this. It is easy to understand the logic of action sheet display. Thanks - Manraj.

gmanraj
  • 41
  • 2
2

The documentation for the -showFromRect:inView: method states this:

On iPad, this method displays the action sheet in a popover whose arrow points to the specified rectangle of the view

This means that you may be able to get it to display the arrow direction you want by fiddling with the rect. However, there's no reason I can think of why you would want to manually set the arrow direction because UIActionSheet will automatically calculate the best arrow direction.

indragie
  • 18,002
  • 16
  • 95
  • 164
  • 2
    Well, it might happen that the sheet appears under the user's hand, which isn't nice. – BastiBen Apr 05 '11 at 09:37
  • 10
    If "iOS knows best", why are permitted arrow directions given as arguments to normal popover API? Short answer: iOS does not know best. – hatfinch Nov 25 '11 at 16:19