代码之家  ›  专栏  ›  技术社区  ›  Simone Margaritelli

使用一个UIViewController和两个XIB在iPad上处理方向更改

  •  9
  • Simone Margaritelli  · 技术社区  · 15 年前

    我想用一个UIViewController和两个xib来处理iPad应用程序的方向更改,比如MenuView和MenuViewLandscape。

    那么,在MenuViewController的willrotatetointerfaceorientitation方法中,如何在不使用另一个控制器进行横向模式的情况下更改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时,横向视图控件没有正确旋转。

    2 回复  |  直到 15 年前
        1
  •  12
  •   Josh Bernfeld    13 年前

    我不确定这个实现有什么奇怪的副作用,但是试试这样的方法,看看它是否适合您:

    -(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构建的,也可以替换 加载名 这些行:

    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紧跟在XIBs中文件的所有者和第一响应者代理对象之后。

    然后您只需要确保视图按照界面方向正确旋转。对于所有不在默认纵向方向的视图,通过设置 transform 视图的属性和使用 CGAffineTransformMakeRotation() 使用上面示例中所示的适当值。

    单独的旋转可以解决你的问题,而不需要额外的笔尖负荷。但是,加载 MenuViewController 并将其视图设置为现有的 菜单控件 的视图可能会导致生命周期和旋转事件的一些奇怪行为,因此尝试上面的示例可能会更安全。它们还帮你省去了创建新的 菜单控件 当你只需要它的视图时。

    希望这有帮助!

    贾斯廷

        2
  •  1
  •   Community Mohan Dere    9 年前

    也许乔恩·罗德里格斯的回答会让你随心所欲:

    Want to use muliple nibs for different iphone interface orientations

    如果您有两个UIViewController类,一个是纵向模式的基类,另一个是横向模式的子类,那么您几乎可以将所有代码放在基类中。因此,这为您提供了单个视图控制器类的大多数优点,同时也允许您使用其他类似的解决方案:

    Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?