2011-04-29 59 views
2

以下是我希望在應用程序中使用的兩種佈局。如果應用程序從縱向切換到橫向時,它會保持UILabels,BOOL和其他對象,那將會很好。由於按鈕的位置不同,我不能只在自動旋轉時使肖像視圖自動調整大小。我也想使用BOOL和右上角的按鈕來實現自己的旋轉鎖定。不同佈局的旋轉 - iOS 4

我想過使用一個- (空)orientationChanged:(NSNotification)通知presentModalViewController然而這些並沒有在對象複製並似乎導致more harm then good and didn't appear to work properly

感謝您的幫助!

Portrait xib Landscape xib

嘗試的解決方案:

我加橫向視圖到ViewController,具有在視圖控制器兩個視圖。我把它連接到我在ViewController的@interface部分添加的UIView * landscapeView下的File's Owner。我在viewDidLoad方法中添加了[self.view addSubview:landscapeView]。然後,我添加了這段代碼:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    if (orientationLock) 
     return NO; 
    else { 
     if (interfaceOrientation == UIInterfaceOrientationPortrait) { 
      [landscapeView setHidden:YES]; 
      //[self.view setHidden:NO]; removed 
     } else { 
      //[self.view setHidden:YES]; removed 
      [landscapeView setHidden:NO]; 
     } 
     return YES; 
    } 
} 

但是,這不是正確的解決方案。當我運行模擬器並旋轉它時,屏幕沒有正確放置。當設備改變方向

Bad Image

回答

1

所有的東西(你的實例變量的值)保持不變。如果你只有一個viewController,並且你同時顯示兩個orieantation,那麼你可以輕鬆地管理這個。我建議你在nib文件中創建兩個UIViews,這樣你就可以做所有你想做的事情了。希望你明白我在說什麼。讓我知道你是否需要幫助。

下面是修改後的代碼

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
if (orientationLock) 
    return NO; 
else { 
    if (interfaceOrientation == UIInterfaceOrientationPortrait) { 
     [landscapeView setHidden:YES]; 

    } else { 

     [landscapeView setHidden:NO]; 
    } 
    return YES; 
} 
} 
+0

我可能需要一些更多的幫助,我很初學者。所以我應該把這兩個視圖合併成一個xib文件。應用程序如何知道何時使用哪個視圖? – michaellindahl 2011-04-29 04:20:58

+0

是當設備改變其方向時,你會得到稱爲shouldRotateToInterfaceOrientation的方法,你可以檢查interfaceOrientation == UIInterfaceOrientationPortrait,然後你可以顯示縱向視圖並隱藏橫向視圖。希望你能理解 – 2011-04-29 16:24:19

+0

你也可以檢查你的國旗的價值,這樣如果國旗的價值是肯定的,那麼只有顯示或隱藏,否則不要做任何事情,一旦國旗變了 – 2011-04-29 16:25:37