【问题标题】:Best way to change UIViewController view NIB programmatically以编程方式更改 UIViewController 视图 NIB 的最佳方法
【发布时间】:2011-05-18 02:52:57
【问题描述】:

我有这个具有不同 NIB 和视图的 iPad 应用程序。 每个视图都有两个 NIB,一个用于纵向,一个用于横向,所以我正在寻找一种方法来以编程方式为给定的 UIViewController 切换 NIB。

现在我在每个控制器的 willRotateToInterfaceOrientation 方法中使用了这个函数:

void UIHandleRotation( UIViewController *controller, NSString *nibName, UIInterfaceOrientation orientation, BOOL transform ){
    double angle = 0.0;

    if( orientation == UIInterfaceOrientationPortrait ) {
        angle = PI * 2 /* 360° */;
    } 
    else if( orientation == UIInterfaceOrientationLandscapeLeft ){
        angle   = PI + PI/2 /* 270 ° */;
        // Each landscape nib is [nib-name]L
        nibName = [NSString stringWithFormat:@"%@L", nibName];
    }    

    [[NSBundle mainBundle] loadNibNamed:nibName owner:controller options:nil];
    if( transform ) {
        controller.view.transform = CGAffineTransformMakeRotation( angle );
    } 
}

但它给了我奇怪的行为(ui 控件位置混乱,插座没有像在界面生成器中那样关联,等等)。

我做错了什么还是有更好的方法来实现它? 谢谢

注意:我没有使用导航控制器,所以我不能使用这个解决方案Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?

【问题讨论】:

    标签: interface-builder ipad orientation rotation


    【解决方案1】:

    您应该只使用一个带有两个视图的 NIB,一个用于带有框架(0,0,768, 1024)的纵向(potraitView)和 另一个带有框架(0,0,1024, 768)的景观(landScapeView)。 并根据您的要求在两个视图(potraitView 和landScapeView)中设置您的控件。

    在此之后,您可以根据设备方向将视图设置为-

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
    landScapeView.hidden = YES;
    portraitView.hidden = NO;
    
    }
    
    else{
    
    landScapeView.hidden = NO;
    portraitView.hidden = YES;
    
    }
        return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
                interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }
    

    嗨,

    我想你想知道,当视图加载时,默认方向是什么,以及如何根据这个方向加载视图。 如果这个问题和你问的一样,那么答案是-

    当您运行应用程序并加载相同的类视图时, - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 方法被自动调用。然后它返回您的设备方向,并根据此方向设置您的视图(使用与上述相同的代码)。 我认为你不需要做更多。

    【讨论】:

    • 那我该如何处理默认的视图插座呢?您能否编辑您的帖子并向我解释创建/绑定到 IB 的整个视图?
    【解决方案2】:

    你不应该换笔尖,你应该换控制器。

    请参阅此答案:Easiest Way to Support Multiple Orientations

    【讨论】:

    • 查看我刚刚添加的注释。
    • 您可以使用导航控制器及其最简单的方法来执行此操作。您不必在界面中公开导航控制器。我链接到的示例没有这样做。用户无法判断正在使用导航控制器。
    【解决方案3】:

    您需要每个 nib 有一个视图,因此您的 UIViewController 将有两个 UIView。一张纵向,一张横向。

    【讨论】:

      【解决方案4】:

      为什么你需要为同一个视图使用 2 个 nib。你应该为不同的方向使用一个 nib。你可以用相同的方法以编程方式处理所需的控件

      willRotateToInterfaceOrientation

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多