我不确定这个实现是否有任何奇怪的副作用,但尝试这样的方法,看看它是否适合您:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(orientation)) {
[[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil];
if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.transform = CGAffineTransformMakeRotation(M_PI);
}
} else if (UIInterfaceOrientationIsLandscape(orientation)){
[[NSBundle mainBundle] loadNibNamed:@"MenuViewLandscape" owner:self options:nil];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
self.view.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
} else {
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
}
}
}
假设 MenuView 和 MenuViewLandscape XIB 中的文件所有者都设置为 MenuViewController,并且视图出口也在这两个 XIB 中设置。使用时应轮流正确重新连接所有插座loadNibNamed
.
如果您正在为 iOS 4 进行构建,您还可以替换loadNibNamed
与这些行:
UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;
and
UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil];
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = landscapeView;
这些假定您要显示的 UIView 立即位于 XIB 中的文件所有者和第一响应者代理对象之后。
然后您只需要确保视图针对界面方向正确旋转。对于所有非默认纵向的视图,通过设置transform
视图的属性和使用CGAffineTransformMakeRotation()
具有适当的值,如上例所示。
仅旋转就可以解决您的问题,而无需额外加载 NIB。但是,加载一个全新的实例MenuViewController
并将其视图设置为现有的MenuViewController
的视图可能会导致生命周期和轮换事件出现一些奇怪的行为,因此尝试上面的示例可能会更安全。它们还省去了您必须创建新的麻烦MenuViewController
当您只需要其中的视图时。
希望这可以帮助!
Justin