【问题标题】:Problems with rotation in landscape and portrait view横向和纵向视图中的旋转问题
【发布时间】:2011-10-17 19:15:47
【问题描述】:

在我的应用程序中,我有 2 个视图,portraitView 和landScapeView。而且我的应用程序包含许多视图...

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

  2. 当我在横向视图中启动应用程序时,纵向视图开始显示..稍后旋转并返回视图变得正确。

我正在使用的代码如下...所以请建议我应该做哪些更改来克服上述问题..

- (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;

【问题讨论】:

  • 为什么要为横向和纵向创建不同的视图?为什么你没有根据你所处的方向固定框架大小?
  • 为了更清楚,您只需要使用一个视图。以编程方式或通过合适的界面生成器设置视图的框架。

标签: iphone objective-c ios


【解决方案1】:

听起来您的 PortraitView 和 LandscapeView 在应用程序的初始启动时都是可见的。上面的代码只有在运行时方向改变时才会执行。如果您坚持对纵向和横向使用单独的视图,那么您还需要在启动时检测方向并适当地显示/隐藏视图。

【讨论】:

    【解决方案2】:

    系统调用 shouldAutorotateToInterfaceOrientation: 的事实并不意味着它会立即这样做。您想使用 willRotateToInterfaceOrientation 和 didRotateFromInterfaceOrientation,并尽可能使用自动调整大小。您还可以子类化布局更新回调以重新排列您的视图。不建议有两个视图并在横向/纵向中显示/隐藏它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2021-12-27
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多