在两个视图之间切换以及从纵向切换到横向时发生 iPad 方向问题,反之亦然
我正在使用两个视图控制器构建一个简单的应用程序,我正在使用 iPhone 模拟器测试代码,一切似乎都工作正常。当我从纵向旋转到横向或从横向旋转到纵向时,会出现问题。这是应用程序的逻辑,应用程序总是以纵向启动,我在第一个视图上有一个按钮可以从 View1 切换到 View2。在 View2 上,我有另一个按钮可以从 View2 切换回 View1。假设我处于纵向模式,我从 View1 切换到 View2,然后将 iPad(在模拟器中)从纵向旋转到横向,当我从 View2 切换回来时,即返回到 View1。 View1屏幕/视图以Portrait方式显示,View2屏幕在后台显示,即View2的一部分在后台显示,我猜是因为View1本来就是Portrait模式的。
问题是..以前是否有人遇到过这个问题,如果有的话,有任何代码可以解决这个问题,其次,我如何在代码中识别设备的方向以及视图的方向。
此方法是切换到View 2:
-(IBAction) switchToView2: (id) sender {
SecondViewController *myViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.view addSubview: myViewController.view];
[UIView commitAnimations];
}
这个方法是切换回View1:
-(IBAction) switchBackToView1:(id) sender {
[self.view removeFromSuperview];
[UIView commitAnimations];
}
I am building a simple app with two View Controllers, I am testing the code using the iPhone Simulator, everything seem to be working fine. The problem happens when I rotate from Portrait to Landscape or from Landscape to portrait. This is the logic of the app, the app always launched in Portrait, I have a button to on the first View to Switch from View1 to View2. On View2 I have another button to switch from View2 back to View1. Let say, I am in Portrait mode, I switch from View1 to View2, then rotate the iPad (in the simulator) from Portrait to Landscape, when I switch back from View2, i.e to go back to View1. View1 screen/view is displayed in Portrait with View2 screen displayed in the background, ie part of View2 is displayed in the background, I guess because View1 was originally in Portrait mode.
The question is.. Has anyone had this issue before, if so, any code to fix this issue, secondly, how can I identify in the code which orientation the device is and which orientation the view is in.
This method is to switch to View 2:
-(IBAction) switchToView2: (id) sender {
SecondViewController *myViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.view addSubview: myViewController.view];
[UIView commitAnimations];
}
This method is to switch back to View1:
-(IBAction) switchBackToView1:(id) sender {
[self.view removeFromSuperview];
[UIView commitAnimations];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从你的代码来看:
这让我相信你的两个视图是 myViewController.view 的子视图。这就解释了为什么它们同时出现。对于不同的视图有单独的视图控制器是有意义的。
From your code:
It makes me believe that your 2 views is a subview of myViewController.view. which explains why they're both showing at the same time. it would make sense to have seperate view controllers for different views.
首先我认为
没有必要:
来自Apple
commitAnimations
标记开始/提交动画块的结束并安排动画的执行。
Interface Builder 中的第二个您是否将控制器的属性设置为 Landscape ?
希望这有帮助(抱歉我的英语不好)
First i think
is not necessary :
From Apple
commitAnimations
Marks the end of a begin/commit animation block and schedules the animations for execution.
second in Interface Builder have you set the property of your controller to landscape ?
Hope this Help (sorry for my bad English)
是的,如果您不在代码中添加任何方向更改处理,就会发生这种情况。查看 本指南介绍如何将视图设置为自动或手动处理方向变化的调整。
或者,如果您确实已经进行了处理,那么可能是因为您添加/删除视图的方式所致。为了更好的处理,我认为你应该尝试 UINavigationController 管理视图的方式。即,您应该使用
pushViewController:animated:
和popViewController:animated:
,而不是addSubview:
和removeFromSuperview:
反而。是的,正如 Yoos 所说,上面的代码中不需要
[UIView commitAnimations]
。希望这有帮助。
Yes, that would happen if you dont put any orientation change handling in your code. Check out this guide on how you can set your view to automatically or manually handle adjustment on orientation change.
Or if you do have handling already, then it may be because of how you are adding/removing your views. For better handling, I think you should try the UINavigationController way of managing views. i.e., instead of
addSubview:
andremoveFromSuperview:
, you should usepushViewController:animated:
andpopViewController:animated:
instead.And yes, as Yoos said,
[UIView commitAnimations]
is not needed in your code above.Hope this helps.