支持多个方向的最简单方法?当应用程序处于横向时,如何加载自定义 NIB? [英] Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?

查看:11
本文介绍了支持多个方向的最简单方法?当应用程序处于横向时,如何加载自定义 NIB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想在其中支持多个方向.我有两个要使用的 .xib 文件,myViewController.xib 和 myViewControllerLandscape.xib.myViewController.xib 存在于项目/资源中,myViewControllerLandscape.xib 存在于项目根目录中.

I have an application in which I would like to support multiple orientations. I have two .xib files that I want to use, myViewController.xib and myViewControllerLandscape.xib. myViewController.xib exists in project/Resources and myViewControllerLandscape.xib exists in the root project directory.

我想要做的是使用单独的 NIB (myViewControllerLandscape.xib) 进行旋转.我尝试在 viewDidLoad l 中检测旋转,就像这样:

What I want to do is use a separate NIB (myViewControllerLandscape.xib) for my rotations. I try detecting rotation in viewDidLoad like this:

if((self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight))
 {
  NSLog(@"Landscape detected!");
  [self initWithNibName:@"myViewControllerLandscape" bundle:nil];

 }

但我可以在 gdb 中看到,当应用程序以横向设备启动时,这不会执行.NSLog 消息不会触发.为什么是这样?我做错了什么?

But I can see in gdb that this isn't executed when the app is started with the device in landscape. The NSLog message doesn't fire. Why is this? What have I done wrong?

此外,如果我明确地将 initWithNibName 函数调用放在 viewDidLoad 方法中,则不会加载该笔尖,而是继续使用 myViewController.xib 文件.我的电话怎么了?我应该指定一个捆绑包吗?

Also, if I explicitly put the initWithNibName function call in the viewDidLoad method, that nib is not loaded, and it continues with the myViewController.xib file. What's wrong with my call? Should I specify a bundle?

谢谢!

推荐答案

您必须加载一个视图,然后检查方向并在需要时加载另一个视图.您在 shouldAutorotateToInterfaceOrientation: 中检查方向,如果您想旋转则返回 yes.

You have to load one view, then check orientation and load another if needed. You check orientation in shouldAutorotateToInterfaceOrientation: returning yes if you want to rotate.

我使用导航控制器来管理转换.如果我有纵向视图并且设备旋转,我推动横向视图,然后在返回纵向时弹出横向视图.

I use a navigation controller to manage the transition. If I have the portrait view up and the device rotates, I push the landscape view and then pop the landscape view when it return to portrait.

我为所有方向返回 YESshouldAutorotateToInterfaceOrientation:但这会在应用程序时调用吗发射?你把你的观点推到里面吗这个功能?

I return YES for all orientations in shouldAutorotateToInterfaceOrientation: but will this be called when the app launches? Do you push your view inside of this function?

方向常量不是您查询的全局变量,而是系统发送给控制器的消息的一部分.因此,您无法在视图控制器加载之前轻松检测方向.相反,您将应用硬连线以特定方向(通常是纵向)启动,然后立即旋转.(请参阅移动版 Safari.它始终以纵向模式开始,然后旋转为横向模式.)

The orientation constants are not globals you query but rather part of the messages sent the controller by the system. As such, you cannot easily detect orientation before a view controller loads. Instead, you hardwire the app to start in a particular orientation (usually portrait) and then immediately rotate. (See mobile Safari. It always starts in portrait and then rotates to landscape.)

这是我用来交换纵向和横向视图的两种方法.

These are the two methods I used to swap out my portrait and landscape views.

三个视图控制器都有这个方法:

All three view controllers have this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

头像是这样的:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
        [self.nav pushViewController:rightLVC animated:NO];
    }
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        [self.nav pushViewController:leftLVC animated:NO];
    }
}

每个景观控制器都有:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
        [self.nav popViewControllerAnimated:NO];
    }

应用以纵向模式启动.如果设备的方向是横向,它会推送适当的横向.当设备旋转回纵向时,它会弹出横向.对于用户来说,它看起来就像是同一个视图为不同的方向重新组织了自己.

The app starts in portrait. If the orientation of the device is landscape, it pushes the appropriate landscapes. When the device rotates back to portrait, it pops the landscape. To the user it looks like the same view reorganizing itself for a different orientation.

这篇关于支持多个方向的最简单方法?当应用程序处于横向时,如何加载自定义 NIB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆