Forcing a UIActionSheet to Use a Specific Orientation

In my application, there is one view controller that cannot change orientation. Think of it as a game board that needs to be kept in place.

Now I want to place the UIActionSheet, but given the way the user holds the device, I would like to map the sheet to the orientation.

It seems that UIActionSheet, unlike, say, MFComposerViewController, does not get its orientation from the StatusBar, which I rotate based on the orientation, but get the orientation from the main view controller. In other words, the UIActionSheet appears in portrait orientation, regardless of how the device is held.

I 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 correct orientation, but the resulting action sheet appears on the side of the portrait and is sized as if it were still in portrait orientation. The worksheet frame is calculated based on the number of buttons, etc.

Has anyone been able to get the UIActionSheet to display correctly in the specified orientation?

(I also tried to create a new view that was converted and had the correct size, but the UIActionSheet ignored it. All that remains was to create a new UIViewController that automatically rotates and has a UIActionSheet, which means that my playing field is completely covered by the new view controller.)

+5
source share
2

, . , UIActionSheets , :

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 pi/2, .

+7

JonasGessner/JGActionSheet, , src.

iOS 8.0.2. :

  • .

JGActionSheet.h JGActionSheet.m

  1. , .

#import "JGActionSheet.h"

  1. .

:

JGActionSheetSection *menuSection = [JGActionSheetSection sectionWithTitle:nil message:@"Select Food" buttonTitles:@[@"Hamburger", @"Toast", @"Rice"] buttonStyle:JGActionSheetButtonStyleDefault];
[menuSection setButtonStyle:JGActionSheetButtonStyleRed forButtonAtIndex:0];

JGActionSheetSection *cancelSection = [JGActionSheetSection sectionWithTitle:nil message:nil buttonTitles:@[@"Cancel"] buttonStyle:JGActionSheetButtonStyleCancel];

NSArray *sections = @[menuSection, cancelSection];
JGActionSheet *sheet = [JGActionSheet actionSheetWithSections:sections];

[sheet setButtonPressedBlock:^(JGActionSheet *sheet, NSIndexPath *indexPath) {
    [sheet dismissAnimated:YES];
}];

// Set specific orientation to action sheet
sheet.transform = CGAffineTransformMakeRotation(M_PI);

[sheet showInView:self.view animated:YES];
0

All Articles