我有兴趣拥有一个支持所有方向并干净地使用两个 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/