如何为同一视图控制器的不同设备方向加载不同的 XIB?
文档说,如果我想同时支持纵向和横向,我基本上有两种方法可以做到这一点:
- 设置视图控制器的视图,以便子视图正确自动调整大小并在运行时以编程方式进行较小的更改
- 如果更改更为实质性,创建替代横向界面并在运行时推送/弹出替代模态视图控制器
我想呈现布局显着不同但逻辑相同的信息。理想情况下,我会为同一个视图控制器加载另一个 XIB,但这似乎不是一个选项。
听起来#2 是我需要做的,但我的问题是,它听起来像是会使用标准的 modalviewcontroller 动画,而这些动画与设备旋转动画完全不同。 (当然,作为一个懒惰的Webber,我没有测试这个假设。)
那么,如何使用相同的视图控制器但不同的 XIB 加载横向的替代布局?我应该使用上面的方法 #2 吗?旋转动画是否自然?或者还有其他办法吗?
The documentation says that if I want to support both portrait and landscape, I basically have two ways of doing that:
- Set up the viewcontroller's view so that the subviews autoresize correctly and make smaller changes programmatically at runtime
- If the changes are more substantial, create an alternative landscape interface and push/pop the alternative modal viewcontroller at runtime
I would like to present the info where the layout is substantially different, but logic is the same. Ideally, I would load another XIB for the same viewcontroller, but it does not seem to be an option.
Sounds like #2 is what I need to do, but my problem with that is that it sounds like it would use the standard modalviewcontroller animations that are nothing like the device rotation animation. (Of course, being the lazywebber that I am, I did not test this hypothesis.)
So, how do I load an alternative layout for landscape with the same viewcontroller but different XIB? Should I use the method #2 above and is the rotation animation natural? Or is there some other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在
-viewDidLoad:
中实例化我的UIView
实例,并将它们作为子视图添加到视图控制器的view
属性中:然后调用
-viewWillAppear :
使这些子视图居中:我还重写了
-willRotateToInterfaceOrientation:duration:
-adjustViewsForOrientation:
方法设置了各种视图的中心CGPoint
子视图对象,具体取决于设备的方向:加载视图控制器时,将根据设备的当前方向创建和定位 UIView 实例。如果随后旋转设备,视图将重新以新坐标为中心。
为了使这一过程更加平滑,人们可以在 -adjustViewsForOrientation: 中使用键控动画,以便子视图更优雅地从一个中心移动到另一个中心。但目前以上内容对我有用。
I instantiate my
UIView
instances in-viewDidLoad:
and add them as subviews to the view controller'sview
property:I then call
-viewWillAppear:
to center those subviews:I also override
-willRotateToInterfaceOrientation:duration:
The
-adjustViewsForOrientation:
method sets the centerCGPoint
of various subview objects, depending on the device's orientation:When the view controller is loaded, the
UIView
instances are created and positioned based on the device's current orientation. If the device is subsequently rotated, the views are re-centered to new coordinates.To make this smoother, one could probably use a keyed animation in
-adjustViewsForOrientation:
, so that the subviews more gracefully move from one center to the other. But the above works for me, for now.