使用一个 UIViewController 和两个 XIB 处理 iPad 上的方向变化
我想用一个 UIViewController 和两个 XIB(假设是 MenuView 和 MenuViewLandscape)来处理 iPad 应用程序上的方向更改。
那么,在 MenuViewController 的 willRotateToInterfaceOrientation 方法中,如何在不使用横向模式的另一个控制器的情况下更改 XIB?
我使用以下代码:
if( toInterfaceOrientation != UIInterfaceOrientationPortrait ){
MenuViewController *landscape = [[MenuViewController alloc]
initWithNibName: @"MenuViewLandscape"
bundle:nil
];
[self setView:landscape.view];
}
else {
MenuViewController *potrait = [[MenuViewController alloc]
initWithNibName: @"MenuView"
bundle:nil
];
[self setView:potrait.view];
}
但是当我转到横向视图 XIB 时,横向视图控件未正确旋转。
i'd like to handle orientation change on an iPad application with one UIViewController and two XIBs, let's say MenuView and MenuViewLandscape.
So, in the willRotateToInterfaceOrientation method of the MenuViewController, how can i change XIB without using another controller for the landscape mode ?
I'm using the following code:
if( toInterfaceOrientation != UIInterfaceOrientationPortrait ){
MenuViewController *landscape = [[MenuViewController alloc]
initWithNibName: @"MenuViewLandscape"
bundle:nil
];
[self setView:landscape.view];
}
else {
MenuViewController *potrait = [[MenuViewController alloc]
initWithNibName: @"MenuView"
bundle:nil
];
[self setView:potrait.view];
}
But when i go to landscape view the XIB the landscape view controls are not properly rotated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定此实现是否有任何奇怪的副作用,但请尝试类似的操作,看看它是否适合您:
这假设您的 MenuView 和 MenuViewLandscape XIB 中的文件所有者都设置为 MenuViewController 并且视图两个 XIB 中也设置了插座。使用
loadNibNamed
时,所有插座都应在轮换时正确重新连接。如果您正在针对 iOS 4 进行构建,您还可以将
loadNibNamed
行替换为以下内容:和
这些假设您要显示的 UIView 紧跟在 XIB 中的文件所有者和第一响应者代理对象之后。
然后您只需要确保视图针对界面方向正确旋转。对于所有非默认纵向的视图,通过设置视图的
transform
属性并使用CGAffineTransformMakeRotation()
以及适当的值来旋转它们,如下所示上面的例子。仅旋转就可以解决您的问题,而无需额外加载 NIB。但是,加载
MenuViewController
的全新实例并将其视图设置为现有MenuViewController
的视图可能会导致生命周期和旋转事件发生一些奇怪的行为,因此您可能会尝试上面的例子更安全。当您只需要其中的视图时,它们还可以为您省去创建新的MenuViewController
实例的麻烦。希望这有帮助!
贾斯汀
I'm not sure there are any strange side-effects with this implementation, but try something like this and see if it works for you:
This assumes that the File's Owner in your MenuView and MenuViewLandscape XIBs are both set to MenuViewController and that the view outlet is set in both XIBs as well. All of your outlets should be reconnected properly on rotation when using
loadNibNamed
.If you are building for iOS 4, you could also replace the
loadNibNamed
lines with these:and
These assume that the UIView that you want to display immediately follows the File's Owner and First Responder proxy objects in the XIBs.
Then you just need to make sure the views are rotated properly for the interface orientation. For all of the views that are not in the default portrait orientation, rotate them by setting the
transform
property of the view and usingCGAffineTransformMakeRotation()
with the appropriate values as shown in the example above.The rotation alone might solve your issue without the extra loading of the NIBs. However, loading a whole new instance of a
MenuViewController
and setting its view to the existingMenuViewController
's view might cause some strange behavior with lifecycle and rotation events, so you might be safer trying the examples above. They also save you the trouble of having to create newMenuViewController
instances when you only need the view from it.Hope this helps!
Justin
也许乔恩·罗德里格斯(Jon Rodriguez)的答案会满足您的要求:
想要对不同的iphone界面方向使用多个笔尖
如果你有两个UIViewController类,一个用于纵向模式的基类和一个用于横向模式的子类,你可以将几乎所有代码放在基类中。因此,这为您提供了单个视图控制器类的大部分优点,同时还允许您使用其他解决方案,如下所示:
支持多个方向的最简单方法?当应用程序处于横向模式时,如何加载自定义 NIB?
Perhaps the answer from Jon Rodriguez here will do what you want:
Want to use muliple nibs for different iphone interface orientations
If you have two UIViewController classes, a base class for portrait mode and a subclass of that for landscape mode, you can put almost all the code in the base class. So that gives you most of the advantages of a single view controller class while also allowing you to use other solutions like this:
Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?