在iPad上的UIInterfaceOrientation期间更改UIViews

我正在尝试改变旋转的观点,因为我的观点必须从纵向到横向显着不同。 现在,我正在使用的代码工作一次,然后应用程序冻结时试图回旋。 无论哪个方向都没有什么区别。 例如:如果我在景观和旋转肖像一切都很好,直到我旋转回景观,然后冻结,没有任何东西。

这是我用来实现这个的代码

在我的“viewDidLoad”方法

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

然后我把这个叫做轮换:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES; } - (void)didRotate:(NSNotification *)notification { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if ((orientation == UIDeviceOrientationLandscapeLeft) || (orientation == UIDeviceOrientationLandscapeLeft)) { // present the other viewController, it's only viewable in landscape [self.view addSubview:landScapeView]; } if ((orientation == UIDeviceOrientationLandscapeRight) || (orientation == UIDeviceOrientationLandscapeRight)) { // present the other viewController, it's only viewable in landscape [self.view addSubview:landScapeView]; } else if ((orientation == UIDeviceOrientationPortrait || (orientation == UIDeviceOrientationPortrait)) { // get rid of the landscape controller [self.view addSubview:portrait]; } else if ((orientation == UIDeviceOrientationPortraitUpsideDown || (orientation == UIDeviceOrientationPortraitUpsideDown)) { // get rid of the landscape controller [self.view addSubview:portrait]; } } 

你正在旋转添加你的方向特定的视图,但不要删除你需要的另一个。

 // get rid of the landscape controller ((orientation == UIDeviceOrientationPortraitUpsideDown || orientation == UIDeviceOrientationPortraitUpsideDown)) { [landScapeView removeFromSuperview]; [self.view addSubview:portrait]; } 

和其他方向类似。

请记住,从超级视图版本中删除,所以你需要从外部保留两个视图。