【问题标题】:Handle orientation change on iPad with one UIViewController and two XIB使用一个 UIViewController 和两个 XIB 在 iPad 上处理方向更改
【发布时间】:2011-05-12 11:36:23
【问题描述】:

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

那么,在 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 的横向视图时,横向视图控件没有正确旋转。

【问题讨论】:

    标签: ipad rotation landscape nib


    【解决方案1】:

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

    -(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 中的 File's Owner 都设置为 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 实例。

    希望这会有所帮助!

    贾斯汀

    【讨论】:

    • 嗨贾斯汀。我已经尝试过您的解决方案,它适用于 Portrait。然而,对于景观来说,一切都是模糊的。旋转正确,但文字和标签模糊,一切都非常模糊。有些线条是锯齿状的。有任何想法吗 ?谢谢。
    【解决方案2】:

    也许 Jon Rodriguez 的回答会如你所愿:

    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?

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      相关资源
      最近更新 更多