2012-10-12 103 views
0

在我的應用程序中,我支持單個ViewController的橫向和縱向。我可以使用Autoresize來支持橫向和縱向。但我需要製作與人像不同的自定義風景。我對iOS很新。在谷歌和搜索了很多搜索,但無法找到解決方案。如何在iOS中支持橫向和縱向視圖?

我正在使用Xcode 4.5和故事板使視圖。

如何支持自定義橫向和縱向視圖?

任何幫助將不勝感激。

回答

2

在.m文件試試這個:

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation 
{ 
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     // Portrait 

     [object setFrame:CGRectMake(...)]; 

     // Do the same for the rest of your objects 
    } 

    else 
    { 
     // Landscape 

     [object setFrame:CGRectMake(...)]; 

     // Do the same for the rest of your objects 
    } 
} 

在功能方面,已定義的每個對象的位置在您看來,對於人像和風景。

然後你在viewWillAppear中調用該函數來初始化它的工作;視圖確定在開始使用哪個方向:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [self updateLayoutForNewOrientation:self.interfaceOrientation]; 
} 

而且,當你旋轉:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 
{  
    [self updateLayoutForNewOrientation:self.interfaceOrientation]; 
} 

這是我採取的辦法,如果我需要考慮的方向更加定製的外觀。希望這會爲你工作。

編輯:

如果您使用兩個UIViews,一個縱向和另一景觀,在一個UIViewController中,你會改變的代碼是第一部分看起來像這樣:

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation 
{ 
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     // Portrait 

     portraitView.hidden = NO; 
     landscapeView.hidden = YES; 
    } 

    else 
    { 
     // Landscape 

     portraitView.hidden = YES; 
     landscapeView.hidden = NO; 
    } 
} 

有這個編輯過的樣本和原始文本之間的優缺點。在原始代碼中,您必須爲每個對象編碼,在此編輯的示例中,此代碼是您需要的全部代碼,但是,您需要基本上分配對象兩次,一次是縱向視圖,另一次是橫向視圖。

+0

感謝您的回答。我有點困惑CGRectMake中發生了什麼。因爲目前我使用故事板製作了兩個視圖。一個用於橫向和其他一個肖像。如何將這兩個視圖鏈接到您的代碼中 – GoCrazy

+0

CGRectMake用於定義接口對象的位置,除了這些對象的大小 - CGRectMake(x位置,y位置,寬度,高度)' - - 我會編輯我的答案,以反映如果您使用兩個UIViews會做什麼。 – Scott

+0

謝謝肖恩,這將是優秀的。很多讚賞 – GoCrazy

1

你仍然可以使用肖恩的方法,但由於你有2個不同的視圖,你可能有2個不同的Xib文件,所以,而不是CGRect部分,你可以做一些像[[NSBundle mainBundle] loadNibNamed:"nibname for orientation" owner:self options:nil]; [self viewDidLoad];。我不完全知道這將如何與故事板,因爲我還沒有使用它,但我在一個需要在方向不同的佈局的應用程序中做到了這一點,所以我創建了2個Xib文件並將它們都連接到了ViewController上在旋轉時加載適當的Xib文件。

相關問題