使用一个 UIViewController 和两个 XIB 处理 iPad 上的方向变化

发布于 2024-10-03 06:42:46 字数 880 浏览 2 评论 0原文

我想用一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-10-10 06:42:46

我不确定此实现是否有任何奇怪的副作用,但请尝试类似的操作,看看它是否适合您:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        [[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil];
        if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            self.view.transform = CGAffineTransformMakeRotation(M_PI);
        }
    } else if (UIInterfaceOrientationIsLandscape(orientation)){
        [[NSBundle mainBundle] loadNibNamed:@"MenuViewLandscape" owner:self options:nil];
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            self.view.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
        } else {
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
        }
    }
}

这假设您的 MenuView 和 MenuViewLandscape XIB 中的文件所有者都设置为 MenuViewController 并且视图两个 XIB 中也设置了插座。使用 loadNibNamed 时,所有插座都应在轮换时正确重新连接。

如果您正在针对 iOS 4 进行构建,您还可以将 loadNibNamed 行替换为以下内容:

UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;

UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil];
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = landscapeView;

这些假设您要显示的 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:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        [[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil];
        if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            self.view.transform = CGAffineTransformMakeRotation(M_PI);
        }
    } else if (UIInterfaceOrientationIsLandscape(orientation)){
        [[NSBundle mainBundle] loadNibNamed:@"MenuViewLandscape" owner:self options:nil];
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            self.view.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
        } else {
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
        }
    }
}

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:

UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;

and

UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil];
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = landscapeView;

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 using CGAffineTransformMakeRotation() 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 existing MenuViewController'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 new MenuViewController instances when you only need the view from it.

Hope this helps!

Justin

当爱已成负担 2024-10-10 06:42:46

也许乔恩·罗德里格斯(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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文