如何防止模态视图在 iPad 上旋转时消失?

发布于 2024-10-17 15:10:54 字数 1961 浏览 2 评论 0原文

我使用 willRotateToInterfaceOrientation 在 iPad 旋转时交换视图。如果我在设备旋转和交换视图时打开模态视图或警报视图,则视图会交换并且警报会消失并且不会重新出现,即使稍后再次“呈现”警报也是如此。

编辑: 我已经缩小了这个问题的范围。当模态视图以 UIModalPresentationFullScreen 呈现时,模态视图“不会”旋转。

我可以做什么来解决这个问题?

这是我的 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];
}   
}

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];
}   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小糖芽 2024-10-24 15:10:54

如果我要解决这个问题,我会执行以下操作之一

  1. 将备用视图添加到父视图,而不更改视图属性
  2. 为不需要完整视图交换但重新排列或隐藏子元素的视图创建设计的观点。
  3. 从层次结构的根 ViewController 呈现任何模式,

我会非常努力地不为了方向而完全交换视图。即使您解决了这个问题,似乎仍会继续出现问题。

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.

極樂鬼 2024-10-24 15:10:54

如果你交换视图,我认为你也应该交换模态视图。

例如,如果您呈现弹出窗口控制器 - 它会自动关闭,然后随着 UI 旋转而出现。

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.

2024-10-24 15:10:54

这里有一个想法:保持主视图不变,但将子视图更改为纵向或横向视图。像这样:

- (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:]
}

所以现在当您创建模态时,它将链接到您的控制器,该控制器现在具有恒定的主视图。

请注意,我没有对此进行测试,因为我在休息两周后又开始编码......
祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文