我想在iOS设备旋转到横向时显示不同的全屏视图,并在设备旋转回横向时返回上一个视图。
我主要是通过使用一个视图控制器和两个视图来实现它,然后在 - shouldAutorotateToInterfaceOrientation中将视图控制器的self.view设置为适当的视图。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
self.view = landscapeView;
}else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
self.view = portraintView;
}
return YES;
}
但是,理想情况下,我希望横向视图拥有自己独立的视图控制器来管理视图。我试图以模式方式推送视图控制器并在shouldAutorotateToInterfaceOrientation:中将其关闭,但是横向视图控制器没有以正确的方向出现(它仍然认为设备是纵向的)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
[self presentModalViewController:landscapeViewController animated:YES];
}else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
[self dismissModalViewControllerAnimated:YES];
}
return YES;
}
然后,当我回到纵向视图时,它仍然认为设备处于风景中。
答案 0 :(得分:1)
您应该在willAnimateRotationToInterfaceOrientation: duration:
或didRotateToInterfaceOrientation:
而不是shouldRotateToInterfaceOrientation
进行轮换工作。然后使用提供的interfaceOrientation
切换您的观看次数。这种方式更加可靠,只有在您实际旋转设备时才会被调用。
答案 1 :(得分:0)
正如@MishieMoo指出的那样,我需要在didRotateToInterfaceOrientation中完成我的工作,以便视图控制器以正确的方向呈现。
所以现在我的肖像视图控制器的代码如下所示:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationPortraitUpsideDown == UIInterfaceOrientationLandscapeRight){
[self performSegueWithIdentifier:@"fullscreenSegue" sender:self];
}
}
我做一个故事板segue来推动全屏视图控制器,但你可以轻松加载视图控制器并执行[self presentModalViewController:landscapeViewController animated:YES]。
在全屏视图控制器中解除视图的代码:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
[self dismissModalViewControllerAnimated:NO];
}
}