【问题标题】:How to load different XIBs for different device orientations for same viewcontroller?如何为同一视图控制器加载不同设备方向的不同 XIB?
【发布时间】:2011-02-10 08:19:01
【问题描述】:

The documentation 说如果我想同时支持纵向和横向,我基本上有两种方法:

  1. 设置视图控制器的视图,以便子视图正确自动调整大小并在运行时以编程方式进行较小的更改
  2. 如果更改更大,create an alternative landscape interface 并在运行时推送/弹出替代模式视图控制器

我想展示布局大不相同但逻辑相同的信息。理想情况下,我会为同一个视图控制器加载另一个 XIB,但这似乎不是一个选项。

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

那么,如何使用相同的视图控制器但不同的 XIB 为横向加载替代布局?我应该使用上面的方法#2,旋转动画是否自然?还是有其他方法?

【问题讨论】:

    标签: ios iphone layout uiviewcontroller rotation


    【解决方案1】:

    我在-viewDidLoad: 中实例化我的UIView 实例并将它们作为子视图添加到视图控制器的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: 中使用键控动画,以便子视图更优雅地从一个中心移动到另一个中心。但目前以上对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-16
      • 2011-06-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多