2010-11-30 107 views
0

我有這個iPad應用程序,具有不同的NIB和視圖。 每個視圖都有兩個NIB,一個用於potrait方向,另一個用於橫向,所以我正在尋找一種以編程方式切換給定UIViewController的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?

回答

1

你不應該切換筆尖,你應該切換控制器。

看到這個答案爲:Easiest Way to Support Multiple Orientations

+0

請參閱我剛剛添加的說明。 – 2010-11-30 20:44:46

+0

您可以使用導航控制器及其最簡單的方式來執行此操作。您不必在界面中暴露導航控制器。我鏈接的例子並不這樣做。用戶無法知道正在使用導航控制器。 – TechZen 2010-11-30 22:18:49

0

你需要有每一個筆尖視圖,因此UIViewController中有兩個UIViews。一個是肖像,另一個是風景。

0

爲什麼你需要使用2個坯相同view.You應使用不同的orientations.You可以在相同的方法處理編程所需的控制一個筆尖

willRotateToInterfaceOrientation

感謝。

3

您應該只使用一個帶有兩個視圖的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); 
} 

嗨設置你的意見,

當我想你想知道,當視圖加載,什麼是默認的方向,以及如何根據此方向加載視圖。 如果這個問題是一樣的,你都在問,那麼answes是 -

當你運行你的應用程序,並且在同一類視圖負荷,然後 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 方法是自動調用。然後它返回您的設備方向,並根據此方向設置您的視圖(使用與上述相同的代碼)。 我認爲你不需要做更多。

相關問題