UIActionSheet Change arrow position?
Asked Answered
P

3

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?

Phraseology answered 21/9, 2010 at 18:32 Comment(0)
T
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.

Thies answered 1/1, 2012 at 13:24 Comment(4)
I feel kinda guilty with the hackyness of this, but it works.Serpasil
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.Contraindicate
I've tried to do this with a BarButtonItem and have had no luck. I tried obtaining the rectangle position from here: https://mcmap.net/q/271362/-figure-out-uibarbuttonitem-frame-in-windowBetoken
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.Betoken
H
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.

Hybridism answered 15/10, 2012 at 19:13 Comment(1)
Useful hint, i was able to tweak above code and got desired results.Lothair
F
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.

Fargone answered 22/3, 2011 at 4:24 Comment(2)
Well, it might happen that the sheet appears under the user's hand, which isn't nice.Disgraceful
If "iOS knows best", why are permitted arrow directions given as arguments to normal popover API? Short answer: iOS does not know best.Nall

© 2022 - 2024 — McMap. All rights reserved.