以编程方式更改 UIViewController 视图 NIB 的最佳方法

发布于 2024-10-05 10:17:23 字数 1176 浏览 5 评论 0原文

我有这个 iPad 应用程序,具有不同的 NIB 和视图。 每个视图都有两个 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 控件位置混乱,插座没有像界面生成器中那样关联,等等)。

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

注意:我没有使用导航控制器,所以我无法使用此解决方案支持多个方向的最简单方法?当应用程序处于横向模式时,如何加载自定义 NIB?

I have this iPad application with different NIBs and views.
Each view has two NIBs, one for potrait orientation and one for landscape, so i'm searching for a method to programmatically switch NIB for a given UIViewController .

Right now i have this function that i'm using in the willRotateToInterfaceOrientation method of each controller :

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 );
    } 
}

But it's giving me strange behaviour (ui controls position messed, the outlets are not associated as in interface builder, and so forth).

Am i doing something wrong or there's just a better way to implement this ?
Thanks

NOTE: I'm not using a navigation controller, so i can't use this solution Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

只有一腔孤勇 2024-10-12 10:17:23

您应该只使用一个具有两个视图的 NIB,一个用于带有框架 (0,0,768, 1024) 的肖像 (potraitView) 和
另一个用于景观(landScapeView),框架为(0,0,1024, 768)。
并根据您的要求在两个视图(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
方法被自动调用。然后它返回您的设备方向,并根据该方向设置您的视图(使用与上述相同的代码)。
我认为你不需要做更多的事情。

You should use only one NIB with two views, one for portrait(potraitView) with frame(0,0,768, 1024)and
another for landscape(landScapeView) with frame(0,0,1024, 768).
And set your controls in both views (potraitView and landScapeView) according to your requirements.

and after this you can set your views according to device orientation as-

- (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);
}

Hi,

As I think you want to know, when the view loads, what is the default orientation, and How I can load the view according to this orientation.
If this question is same as you are asking, then the answes is-

When you run your application and the same class view loads, then - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method is called automatically. Then it returns your device orientation, and sets your view according to this orientation(using the same code as described above).
I think you need not to do more.

留蓝 2024-10-12 10:17:23

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

请参阅此答案:支持多个方向的最简单方法

You shouldn't switch nibs, you should switch controllers.

See this answer for: Easiest Way to Support Multiple Orientations

人生戏 2024-10-12 10:17:23

您需要每个笔尖有一个视图,因此您的 UIViewController 将有两个 UIView。一张用于肖像,一张用于风景。

you need to have one view per nib, so your UIViewController will have two UIViews. one for portrait, and one for landscape.

无边思念无边月 2024-10-12 10:17:23

为什么同一个视图需要使用 2 个笔尖。您应该为不同的方向使用 1 个笔尖。您可以在相同的方法

willRotateToInterfaceOrientation

中以编程方式处理所需的控件,谢谢。

Why do you need to use 2 nibs for the same view.You should use one nib for different orientations.You can handle the required controls programatically in the same method

willRotateToInterfaceOrientation

Thanks.

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