iPhone旋转时更改视图?

当iPhone处于标准的“肖像”方向时,如何查看一个视图,并在旋转到横向时切换到另一个视图(如图表或其他)?反之亦然。

禁用该视图的方向(假设第一个视图是横向)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 

然后将其添加到viewDidAppear

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]]; 

并在某处添加此方法

 - (void) orientationChanged:(NSNotification *)note { UIDevice * device = note.object; switch(device.orientation) { case UIDeviceOrientationPortrait: // Present View Controller here break; default: break; }; } 

而在另一种观点做同样的,但倒退与景观,而不是现在的肖像。

不要忘记注销通知。

(或者使用两个控件但没有栏的导航视图,只需显示一个你想要的取决于使用的方向)

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

首先阅读UIViewControllers如何工作: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

然后,在你的UIViewController子类中,利用willRotateToInterfaceOrientation:duration:改变你的视图。

例如

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // if portrait [self.landscapeView removeFromSuperview]; [self.view addSubview:self.portraitView]; // if landscape [self.portraitView removeFromSuperview]; [self.view addSubview:self.landscapeView]; } 

并添加适当的if语句或switch case以确定要执行的操作。