如何为同一个viewcontroller为不同的设备方向加载不同的XIB?

Jaa*_*nus 7 iphone layout rotation uiviewcontroller ios

文档说如果我想支持肖像和风景,我基本上有两种方法:

  1. 设置viewcontroller的视图,以便子视图正确自动调整大小并在运行时以编程方式进行较小的更改
  2. 如果更改更具实质性,请创建备用横向界面并在运行时推送/弹出备用模式视图控制器

我想提供布局大不相同的信息,但逻辑是相同的.理想情况下,我会为同一个viewcontroller加载另一个XIB,但它似乎不是一个选项.

听起来像#2是我需要做的,但我的问题是听起来它会使用标准的modalviewcontroller动画,它们与设备旋转动画完全不同.(当然,作为我的懒惰人,我没有测试这个假设.)

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

Ale*_*lds 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];
}
Run Code Online (Sandbox Code Playgroud)

然后我调用-viewWillAppear:将这些子视图居中:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]];
}
Run Code Online (Sandbox Code Playgroud)

我也覆盖-willRotateToInterfaceOrientation:duration:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration {
    [self adjustViewsForOrientation:newInterfaceOrientation];
}
Run Code Online (Sandbox Code Playgroud)

该方法根据设备的方向-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);
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

加载视图控制器时,UIView将根据设备的当前方向创建和定位实例。如果随后旋转设备,视图将重新以新坐标为中心。

为了使这一过程更加平滑,可以在 中使用键控动画-adjustViewsForOrientation:,以便子视图更优雅地从一个中心移动到另一个中心。但目前以上内容对我有用。