iphone - 如何使用 xib 文件支持纵向和横向方向?

标签 iphone ipad interface-builder

我有兴趣拥有一个支持所有方向并干净地使用两个 xib 文件来设置其 subview 的 UIViewController。

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

有更好的方法吗?

最佳答案

经过更多研究,我无法找到一个优雅的解决方案。由于每次旋转时从 xib 文件重新加载元素似乎很慢,并且消除了任何基于动态 View 的数据,因此我切换到基于代码的设置(即不再有 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: 方法调用的,并传递新的方向。

关于iphone - 如何使用 xib 文件支持纵向和横向方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4131027/

相关文章:

iphone - 我们可以一次使用排序描述符和@distinctUnionOfObjects吗?

objective-c - 在框架内使用类时 IB_DESIGNABLE 构建失败

macos - 奇怪的 XCode 7 Interface Builder 警告

ios - 如何在 Storyboard 场景中嵌入自定义 View xib?

ios - 使用背景图像处理新的 iPhone 屏幕尺寸

iphone - 在 UITableView 标题中添加 UIButton

iPhone 图标大小

css - 摆脱 iPad1 上的默认样式(边框半径)

objective-c - 如何获取用户当前的UITextField的属性名称作为字符串?

objective-c - 使用 UIButton 截取 UIView 的屏幕截图