iPhone UIActionSheet auto-rotating not working

2019-06-22 12:30发布

I read a lot about that. People say it will not autorotate whene its parent is not set to auto rotate. I tried everything but no luck. I created view-based app (v4.2) with a button that executes this:

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];

The root controller is set to auto-rotate. The actionSheet does not. Note that when I rotate the simulator none of the root controller's orientation methods are called. Is there a problem with the delegate? What is wrong?

2条回答
爷、活的狠高调
2楼-- · 2019-06-22 13:06

Well, here's my solution to this problem:

Basically what we do is:

  1. Listen to the rotation event.
  2. Listen to click event.
  3. Dismiss the actionSheet and present it again after the rotation is done. (we need to wait a small delay in order for it to take.

for example:

@interface ViewController ()
{
    UIActionSheet *_sheet;
    BOOL _isClicked;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (IBAction)click:(id)sender
{
    _isClicked = YES;

    [self showActionSheet];
}

- (void)didRotate:(NSNotification *)note
{
    [_sheet dismissWithClickedButtonIndex:1 animated:YES];
    [self performSelector:@selector(showActionSheet) withObject:nil afterDelay:1.0];
}

- (void)showActionSheet
{
    if (!_isClicked) return;

    _sheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"new" otherButtonTitles:@"bla", nil];
    [_sheet showInView:self.view];
}
查看更多
We Are One
3楼-- · 2019-06-22 13:07

I found that I was running into this problem when I was presenting the action sheet in a delegate method from a subordinate view (which I had pushed using the navigation controller). The problem was that my view was not the current one, the subordinate view was still up at the point where I was trying to show the action sheet.

By changing my code a little bit so that the delegate method made a note of the interaction needed with the user, and deferring the action sheet presentation to this view’s viewDidAppear method, the sheet appeared at the proper time in the logical interface animation, and the auto-rotation problem went away. You might want to see if that helps you.

In other words, the flow became:

  • Subordinate view called delegate method to report choices the user made when leaving.
  • Parent view recorded this information for later.
  • Navigation controller was told to pop off subordinate view.
  • Parent view’s viewDidLoad: method detected the note made in the delegate method.
  • Action sheet was presented; rotation was now correct.
查看更多
登录 后发表回答