我不确定这种实现是否会有任何奇怪的副作用,但可以尝试类似这样的内容并查看它是否适用于你:
-(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中的File's Owner均设置为MenuViewController,并且在两个XIB中都设置了view outlet。当使用loadNibNamed
时,所有输出应该在旋转时正确重新连接。
如果您正在构建适用于iOS 4的应用程序,您还可以将loadNibNamed
行替换为以下内容:
UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil]
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0]
self.view = portraitView
并且
UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil]
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0]
self.view = landscapeView
假设你想要展示的UIView是直接跟随XIB中的File's Owner和First Responder代理对象。
然后,您只需要确保视图在界面方向上正确旋转。对于所有不在默认纵向方向上的视图,通过设置视图的transform
属性,并使用适当的值调用CGAffineTransformMakeRotation()
方法进行旋转,如上例所示。
仅仅旋转可能会解决您的问题,而不需要加载整个新的MenuViewController实例并将其视图设置为现有的MenuViewController视图,因为这样做可能会导致一些奇怪的生命周期和旋转事件问题,因此尝试上述示例可能更安全。它们还可以节省创建新MenuViewController实例时只需要其视图而不是完整实例的麻烦。
希望这能帮到您!
Justin