代码之家  ›  专栏  ›  技术社区  ›  Jack

iOS-旋转视图显示背景

  •  1
  • Jack  · 技术社区  · 14 年前

    我已经创建了一个视图,希望它能够旋转。这两个视图是: containerView 这有一个 .backgroundColor 红色的 BackgroundImage 作为一个子视图。这是我的旋转代码:

    - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
      [self adjustViewsForOrientation:toInterfaceOrientation];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      return YES;
    }
    
    - (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        backgroundImage.image = [UIImage imageNamed:@"landscape.jpg"];
        backgroundImage.frame = CGRectMake(0, 0, 1024, 704);
        containerView.frame = CGRectMake(0, 0, 1024, 704);
        self.title = @"landscape";
      } else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        backgroundImage.image = [UIImage imageNamed:@"portrait.jpg"];
        backgroundImage.frame = CGRectMake(0, 0, 768, 960);
        containerView.frame = CGRectMake(0, 0, 768, 960);
        self.title = @"portrait";
      }
    }
    

    问题是图像旋转,但背景色在视图旋转时显示。有没有一个很好的解决这个问题的方法(我知道我可以创建图像混合成一种颜色,并将背景设置为相同的颜色,但这不是我想要的)。

    在这里可以看到问题的视频: http://tinypic.com/r/2quj24g/6

    PS图像来自 OmniGroup GitHub repo 只用于演示。

    2 回复  |  直到 13 年前
        1
  •  2
  •   conradev    14 年前

    此问题的解决方案是设置uiview或uiwindow的 backgroundColor [UIColor blackColor] . 当你旋转Safari,同样的事情发生了,只是背景色是黑色的,就像它通常应该是。iPad上的所有应用程序都是这样旋转的,用户不会注意到任何不同。

    PS-而不是使用 willRotateToInterfaceOrientation:duration: 方法、用途 willAnimateRotationToInterfaceOrientation:duration:

        2
  •  0
  •   Michael Morrison    13 年前

    您可以使图像大于屏幕大小。因此,在iPhone上,将图像设为480 x 480。或者在您的情况下,1024 x 1024。