Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing UIActionSheet to use a specific orientation

My app has one view controller that cannot change orientation. Think of it as a playing field that needs to stay fixed in place.

I now want to put up a UIActionSheet, but given the way the user is holding the device, I'd like to match the sheet to the orientation.

It seems that the UIActionSheet, unlike, say, the MFComposerViewController, doesn't get its orientation from the StatusBar, which I am rotating based on orientation, but gets its orientation from the underlying view controller. In other words, the UIActionSheet comes up in portrait orientation, regardless of how the device is being held.

I have tried:

CGAffineTransform   t = CGAffineTransformMakeRotation(lastUIAngle);
[actionSheet setTransform:t];
if (UIInterfaceOrientationIsLandscape(lastOrientation))
    actionSheet.frame = CGRectMake(0, 0, 480, 320);
else 
    actionSheet.frame = CGRectMake(0, 0, 320, 480);
actionSheet.center = self.view.center;

And I manage to get the right orientation, but the resulting action sheet comes up from the portrait side, and is sized as if it were still in portrait orientation. The action sheet's frame is calculated based on the number of buttons, etc.

Has anyone managed to force UIActionSheet to properly display in a specified orientation?

(I also tried creating a new view, which was transformed and had the correct size, but UIActionSheet ignored it. What remains left is to create a new UIViewController which auto rotates, and have UIActionSheet be a child of it. However, that means that my playing field gets completely obscured by the new view controller.)

like image 435
mahboudz Avatar asked Nov 11 '11 21:11

mahboudz


1 Answers

Turns out that the answer was pretty simple. Add this code to force UIActionSheets to whatever orientation you'd like:

tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
tempView.transform = CGAffineTransformMakeRotation(lastUIAngle);
tempView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view.window addSubview:tempView];

UIActionSheet *actionSheet = ...

[actionSheet showInView:tempView];

lastUIAngle is a multiple of pi/2, which, in this case, I had saved from the last time the orientation was changed.

like image 125
mahboudz Avatar answered Oct 03 '22 20:10

mahboudz