Apple 的 NIB 本身不支持旋转,因此我尝试对同一 View 使用两个不同的 NIB - 每个方向一个。
我尝试了以下方法,它有效,除了......它破坏了一些 subview (例如Apple的OpenGL/GLKit View )。
它只会破坏第一次旋转 - 所有后续旋转都完全按预期工作。所以,我假设苹果的“viewWill/Did”调用堆栈中有一个我在这里忽略的微妙之处。问题是,我从来没能找到这方面的真正文档 - 只是在 Apple 文档中模糊地引用了“某些”发生的调用以及何时/为何发生。
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if( UIInterfaceOrientationIsLandscape( toInterfaceOrientation) )
{
[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])] owner:self options:nil];
[self viewDidAppear:FALSE];
[self viewWillDisappear:FALSE];
[self viewDidReload];
[self viewWillAppear:FALSE];
[self viewDidAppear:FALSE];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar-landscape"] forBarMetrics:UIBarMetricsDefault];
}
else
{
[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@", NSStringFromClass([self class])] owner:self options:nil];
[self viewDidAppear:FALSE];
[self viewWillDisappear:FALSE];
[self viewDidReload];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar-portrait"] forBarMetrics:UIBarMetricsDefault];
[self viewWillAppear:FALSE];
[self viewDidAppear:FALSE];
}
}
最佳答案
根据苹果公司的说法,在你的情况下,你不仅应该加载不同的 Nib ,还应该更改 View Controller :
If you want to present the same data differently based on whether a device is in a portrait or landscape orientation, the way to do so is using two separate view controllers.
阅读View Controller Programming Guide了解具体情况。
希望这有帮助!
关于ios - 如何在自动旋转期间加载新的 NIB 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13903184/