首页 文章

横向和纵向视图中的旋转问题

提问于
浏览
1

在我的应用程序中,我有2个视图,portraitView和landScapeView . 我的申请也有很多观点......

  • 当我启动应用程序时,横向和纵向视图将并排显示 .

  • 当我在landscapeview中启动应用程序时,肖像视图正在显示...旋转后再返回视图正在变得正确 .

我正在使用的代码如下所示......所以请告诉我应该采取哪些措施来克服上述问题 .

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{ 

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {  
    NSLog(@"LANDSCAPE>>");
    portraitView.hidden = YES;
    landscapeView.hidden = NO;

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic_landscape.jpg"]];
    self.view.backgroundColor = background;



}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
    NSLog(@"LANDSCAPE<<");
    portraitView.hidden = YES;
    landscapeView.hidden = NO;
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic_landscape.jpg"]];
    self.view.backgroundColor = background;

    self.view.hidden = NO;

}
else{
    portraitView.hidden = NO;
    landscapeView.hidden = YES;


    NSLog(@"Portrait");
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic.jpg"]];
    self.view.backgroundColor = background;

    background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic2.jpg"]];
  self.view.backgroundColor = background;

}

return YES;

2 回答

  • 1

    听起来您的portraitView和landscapeView在应用程序的初始启动时都可见 . 上述代码仅在运行期间方向更改时执行 . 如果您坚持使用纵向和横向的单独视图,那么您还需要在启动时检测方向并适当地显示/隐藏视图 .

  • 1

    系统调用shouldAutorotateToInterfaceOrientation这一事实并不意味着它会在那时再这样做 . 您希望使用willRotateToInterfaceOrientation和didRotateFromInterfaceOrientation,并尽可能使用自动调整大小 . 您还可以对布局更新回调进行子类化以重新排列视图 . 建议不要使用两个视图并显示/隐藏它们以用于横向/纵向 .

相关问题