代码之家  ›  专栏  ›  技术社区  ›  Tyler

如何用XIB文件支持纵向和横向方向?

  •  3
  • Tyler  · 技术社区  · 15 年前

    一种可能是使用NSBundle的 loadNibNamed: 方法在每次旋转时加载新的视图层次结构,但这似乎效率低下,并可能产生不幸的副作用。例如,从nib文件重新加载后,我的所有视图都将失去其先前的状态,例如向文本字段添加文本等。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Tyler    15 年前

    经过更多的研究,我没能找到一个优雅的解决办法。因为在每个旋转中从XIB文件重新加载元素似乎很慢,并且消除了任何基于动态视图的数据,所以我切换到基于代码的设置(即,不再有XIB文件)。

    // This is for an iPad root-level view controller.
    - (void)setupForOrientation:(UIInterfaceOrientation)orientation {
      if (UIInterfaceOrientationIsPortrait(orientation)) {
        CGRect bounds = CGRectMake(0, 0, 768, 1004);
        bkgImageView.frame = bounds;
        // Other view positioning for portrait.
      } else {
        CGRect bounds = CGRectMake(0, 0, 1024, 748);
        bkgImageView.frame = bounds;
        // Other view positioning for landscape.
      }
      [self drawBackgroundForOrientation:orientation];
    }
    

    此方法是从 shouldAutorotateToInterfaceOrientation:

        2
  •  1
  •   Deepak G M    13 年前

    不要从shouldAutoRotatePointerFaceOrientation调用您的函数,在这里您只需要说“是”或“否”,而是应该在委派函数中真正执行您的工作,如willRotateToInterfaceOrientation,它意味着执行您正在做的事情。