iOS 8中添加到窗口的视图旋转问题

6
我已经为视图旋转创建了类别,还在控制器视图和窗口上添加了视图(用于显示自定义指示器)。 我的代码在xCode 5.X 和 xCode6-beta6版本的7.X上都正常工作。但是,在IOS 8 beta上不太正常。 似乎与旧版ios一样,窗口旋转不起作用。
请问您有什么建议吗? 非常感谢!
我的代码如下:
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration {
 if(!self.isIpad)
{

    [menu.view rotateViewToOrientation:newStatusBarOrientation animated:YES];
    if(newStatusBarOrientation==UIInterfaceOrientationPortrait || newStatusBarOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        if (IS_IPHONE_5) {
            menu.view.frame= CGRectMake(0, 518, 320, 50);
        } else {
            menu.view.frame= CGRectMake(0, 430, 320, 50);
        }

    }  else if(newStatusBarOrientation==UIInterfaceOrientationLandscapeLeft) {

        if (IS_IPHONE_5) {
            menu.view.frame= CGRectMake(270,0,50,568);
        } else {
            menu.view.frame= CGRectMake(270,0,50,480);
        }

    } else {

        if (IS_IPHONE_5) {
            menu.view.frame= CGRectMake(0,0,50,568);
        } else {
            menu.view.frame= CGRectMake(0,0,50,480);
        }
    }
    [menu reloadMenu];
  }
}


//Method of Menu Class for orientation change setup
- (void)rotateViewToOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated {
CGPoint center = self.center;
CGRect bounds = self.bounds;
CGAffineTransform transform = CGAffineTransformIdentity;
int intOri=orientation;
switch (intOri) {
    case UIInterfaceOrientationPortraitUpsideDown:
        transform = CGAffineTransformMakeRotation(M_PI);
        break;
    case UIInterfaceOrientationLandscapeLeft:
        transform = CGAffineTransformMakeRotation(-M_PI_2);
        break;
    case UIInterfaceOrientationLandscapeRight:
        transform = CGAffineTransformMakeRotation(M_PI_2);
        break;
    case UIInterfaceOrientationPortrait:
        transform = CGAffineTransformMakeRotation(0);
        break;

}
if (animated) {
    [UIView beginAnimations:nil context:nil];
}
self.transform = transform;
bounds = CGRectApplyAffineTransform(bounds, transform);
self.bounds = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
self.center = center;
 if (animated) {
    [UIView commitAnimations];
 }
}

请粘贴一些代码并展示您遇到的问题,是否有任何函数未被调用或其他任何问题? - Jageen
@AshwinKanjariya,我不明白为什么你需要手动处理视图的框架。你听说过“自动布局”或“自动调整大小掩码”吗?我建议你尽快学习它们,因为它们非常重要。 - holex
@holex,实际上这是太旧的代码(自IOS 3.X以来),已经有大约4年了。而且应用程序足够大,所以我不能为每个类使用自动布局甚至自动调整大小掩码。希望你能理解。我只想修复与视图相关的问题以支持IOS 8。 - Kanjariya-IOS
@AshwinKanjariya,这很有道理,但是定期的年度维护应该涵盖每个主要iOS版本和每个架构之间的更新 - 但我不确定其他开发者的更新策略,我的个人经验是如果缺少这一步骤,应用程序通常会崩溃。 - holex
@holex,感谢您的建议。但是在这个应用程序中,已经有15多个开发人员参与并移动了位置,现在我是唯一一个需要使其兼容IOS-8的人。 对我来说,问题似乎与窗口旋转有关...因为当我将“加载...”视图添加到窗口时,它也没有正确地反映出来...您能否提供更好的方法?感谢您的支持,并期待更好的解决方案。 :) - Kanjariya-IOS
显示剩余2条评论
3个回答

7
我尝试了很多解决方法...并且通过打补丁的方式使其工作。
{
[self.window setBounds:temp];
[menu.view setFrame:CGRectMake(0, self.window.frame.size.height-50, self.window.frame.size.width, 50)];
[menu.view removeFromSuperview];
[self.window addSubview:menu.view];
[self.window makeKeyAndVisible];
}

对于所有设备模式,针对iOS 8,将旋转角度重置为0度的CGAffineTransformMakeRotation并不是一个恰当的解决方案,我知道这一点,但我没有其他替代方式,因此正在等待一个好的答案。


4

在我的情况下,将所有方向的旋转角度设置为0就可以解决问题。 在iOS 8之前,UIWindow坐标始终处于纵向方向,因此您必须自己处理旋转。 在iOS 8中不再需要这样做。 至于为什么要将UIView添加到UIWindow中,我这样做是为了显示警报,无论当前视图层次结构如何,我都希望它显示在其他所有内容的顶部。


1

我遇到了类似的问题。令人惊讶的是,解决方法是注释掉Appdelegate中与self.window相关的所有代码。

注释掉初始化self.window和其赋值。在Attributes检查器中勾选“Is Initial View Controller”复选框来设置Storyboard中的初始视图。

新的更好的解决方案
注释掉self.window会导致其他问题。相反,以下代码是最佳选择:

- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification 
    {
         [UIViewController attemptRotationToDeviceOrientation];
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接