2010-04-20 49 views
6

The documentation说,如果我想支持纵向和横向的,我基本都这样​​做的方法有两种:如何为同一个viewcontroller为不同的设备方向加载不同的XIB?

  1. 设置的视图控制器的看法,这样子视图正确地自动调整大小和程序在运行时
  2. 做更小的变化
  3. 如果变化较大幅度,create an alternative landscape interface和压入/弹出的替代模态的ViewController在运行时

我想呈现信息在布局基本上是不同的,但逻辑是相同的。理想情况下,我会为同一个viewcontroller加载另一个XIB,但它似乎不是一个选项。

听起来像#2是我需要做的,但我的问题是,它听起来像它会使用标准的modalviewcontroller动画,是什么都没有像设备的旋转动画。 (当然,作为我的懒惰网页,我没有测试这个假设。)

那么,如何使用相同的viewcontroller但不同的XIB加载横向的替代布局?我应该使用上面的方法#2,并且旋转动画是自然的吗?或者有其他方法吗?

回答

1

我实例我UIView实例中-viewDidLoad:,并将其添加为子视图视图控制器的view属性:

- (void) viewDidLoad { 
    [super viewDidLoad]; 

    self.myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 210.0f)] autorelease]; 
    // ... 
    [self.view addSubview:myView]; 
} 

我再打电话-viewWillAppear:居中那些子视图:

- (void) viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]]; 
} 

我还覆盖-willRotateToInterfaceOrientation:duration:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration { 
    [self adjustViewsForOrientation:newInterfaceOrientation]; 
} 

-adjustViewsForOrientation:方法设置各种子视图的对象的中心CGPoint,这取决于设备的方向:

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation { 
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { 
     myView.center = CGPointMake(235.0f, 42.0f); 
     // ... 
    } 
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     myView.center = CGPointMake(160.0f, 52.0f); 
     // ... 
    } 
} 

当视图控制器被加载,UIView实例被创建并定位基于所述设备的当前取向。如果设备随后旋转,视图将重新居中到新坐标。

为了使这个更平滑,可以使用-adjustViewsForOrientation:中的键控动画,以便子视图更加优雅地从一个中心移动到另一个中心。但是现在,上面的这些对我来说很有用。

相关问题