开发者

How do I stop a modal view from disappearing when rotating on iPad?

开发者 https://www.devze.com 2023-02-10 20:08 出处:网络
I\'m using willRotateToInterfaceOrientation to swap views when my iPad rotates. If I have a modal view or an alert view open when my device rotates and swaps views, the view swaps and the alert disapp

I'm using willRotateToInterfaceOrientation to swap views when my iPad rotates. If I have a modal view or an alert view open when my device rotates and swaps views, the view swaps and the alert disappears and does not reappear, even if the alert is "presented" again later.

Edit: I've narrowed this problem a bit. When a modal view is presented with UIModalPresentationFullScreen, the modal view "survives" rotations.

What can I do to fix this?

Here is my implementation of willRotateToInterfaceOrientation:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

//
//  Load an alternate view depending on the orientation
//


if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {

    [UIView beginAnimations:@"" context:nil];
    [self setView:theLandscapeView];
    self.view.bounds = CGRectMake(0, 0, 1024, 768);
    self.view.transform = CGAffineTransformMakeRotation(kDegreesToRadians * (-90));
    [UIView commitAnimations];      

}else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {

    [UIView beginAnimations:@"" context:nil];
    [self setView:theLandscapeView];
    self.view.bounds = CGRectMake(0, 0, 1024, 768); 
    self.view.transform = CGAffineTransformMakeRotation(kDegreesToRadians * (90));
    [UIView commitAnimations];

}else 开发者_开发知识库if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {

    [UIView beginAnimations:@"" context:nil];
    [self setView:thePortraitView];
    self.view.transform = CGAffineTransformMakeRotation(kDegreesToRadians * (0));
    self.view.bounds = CGRectMake(0, 0, 768, 1024);
    [UIView commitAnimations];

}else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

    [UIView beginAnimations:@"" context:nil];
    [self setView:thePortraitView];
    self.view.transform = CGAffineTransformMakeRotation(kDegreesToRadians * (180));
    self.view.bounds = CGRectMake(0, 0, 768, 1024);
    [UIView commitAnimations];
}   
}


If I were solving this problem, I would do one of the following

  1. Add the alternate views to a parent view, and not change the view property
  2. Create a design for the views that does not require a full view swap, but rearranges or hides subelements of the view.
  3. Present any modal from the root ViewController of the hierarchy

I would work very hard not to swap out the view entirely for the sake of orientation. It seems like something that will continue to present problems even after you have solved this one.


If you swap views, you should also swap modal views I think.

For example, if you present popover controller - it'll automatically dismissed and then appeared with UI rotation.


Here's an idea: keep you main view constant, but change the subview to your portrait or landscape view. something like:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    // Remove the current subview from the main view
    if (self.view.subviews.count) {
        [self.view.subviews objectAtIndex:0] removeFromSuperview];
    }

    // Use your if-else block, but change [self setView:] for [self.view addSubview:]
}

So now when you create your modal, it will be linked to your controller, which now has a constant main view.

Note that I didn't test this, as I'm getting my head back into coding after two weeks off... Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号