Best way to change UIViewController view NIB programmatically

房东的猫 提交于 2019-12-23 04:53:36

问题


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?


回答1:


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

See this answer for: Easiest Way to Support Multiple Orientations




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/4318190/best-way-to-change-uiviewcontroller-view-nib-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!