2011-01-12 72 views
3

問題: 我有兩個視圖控制器加載到根視圖控制器。兩個子視圖佈局都會響應方向更改。我使用[UIView transformationFromView:...]在兩個視圖之間切換。兩個子視圖的工作對自己好的,但如果...隱藏UIView方向更改/佈局問題

  1. 意見被交換
  2. 取向的變化
  3. 意見再次交換

以前隱藏的視圖有嚴重的佈局問題。我越重複這些步驟,問題越嚴重。

實施細則

我有三個viewsControllers。

  1. MyAppViewController
  2. A_ViewController
  3. B_ViewController

甲&乙ViewControllers各自具有背景圖像,並分別一個UIWebView和AQGridView。爲了給你如何我建立的一切,例如,下面是爲A_ViewController的方法的loadView ...

- (void)loadView { 

    [super loadView]; 

    // background image 
    // Should fill the screen and resize on orientation changes 
    UIImageView *bg = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
    bg.contentMode = UIViewContentModeCenter; 
    bg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    bg.image = [UIImage imageNamed:@"fuzzyhalo.png"]; 
    [self.view addSubview:bg]; 

    // frame for webView 
    // Should have a margin of 34 on all sides and resize on orientation changes 
    CGRect webFrame = self.view.bounds; 
    webFrame.origin.x = 34; 
    webFrame.origin.y = 34; 
    webFrame.size.width = webFrame.size.width - 68; 
    webFrame.size.height = webFrame.size.height - 68; 

    projectView = [[UIWebView alloc] initWithFrame:webFrame]; 
    projectView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

    [self.view addSubview:projectView]; 

} 

爲了簡潔起見,在B_ViewController的AQGridView設置幾乎相同的方式。

現在這兩個視圖都可以正常工作。但是,我使用這兩者在這樣的AppViewController ...

- (void)loadView { 

     [super loadView]; 

     self.view.autoresizesSubviews = YES; 
     [self setWantsFullScreenLayout:YES]; 

     webView = [[WebProjectViewController alloc] init]; 
     [self.view addSubview:webView.view]; 

     mainMenu = [[GridViewController alloc] init]; 
     [self.view addSubview:mainMenu.view]; 

     activeView = mainMenu; 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchViews:) name:SWAPVIEWS object:nil]; 


    } 

和我切換betweem用我自己的方法SWITCHVIEW這樣

- (void) switchViews:(NSNotification*)aNotification; 
{ 
    NSString *type = [aNotification object]; 

    if ([type isEqualToString:MAINMENU]){ 
     [UIView transitionFromView:activeView.view toView:mainMenu.view duration:0.75 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil]; 
     activeView = mainMenu; 
    } 

    if ([type isEqualToString:WEBVIEW]) { 
     [UIView transitionFromView:activeView.view toView:webView.view duration:0.75 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil]; 
     activeView = webView; 
    } 

    // These don't seem to do anything 
    //[mainMenu.view setNeedsLayout]; 
    //[webView.view setNeedsLayout]; 

} 

我摸索着我的方式,通過這兩種觀點這個,我懷疑我所做的很多事情都是錯誤地實現的,所以請隨時指出應該以不同方式完成的任何事情,我需要輸入。

但我主要關心的是瞭解導致佈局問題的原因。下面是該說明的佈局問題的性質兩幅圖像...

UPDATE: 我注意到,當方向是橫向,過渡垂直翻轉,當我希望它是水平的。我不知道這是什麼可能出錯的線索。

Normal Layout

切換到另一種觀點...改變方向....切換回....

Problem Layout

回答

1

我只是碰到這種迷迷糊糊中有你,因爲有一種感覺想通了。但爲了萬一,或者其他人在尋找相同的答案,這就是我在這種情況下所採取的方法。

在各視圖控制器的頭文件(你之間,而不是根MyAppViewController切換的那些)中,添加一個浮動:

float boundWidth; 

在viewDidLoad中,將其初始化爲0

boundWidth = 0; 

然後創建如下所示的檢查:

- (void)viewDidAppear:(BOOL)animated { 
// check to see what orientation the device is in; 
    if ((boundWidth != 0) && (boundWidth != self.view.bounds.size.width)) { 
     // the orientation has changed, so insert code to deal with this; 
} 

通過設置時的跟蹤寬度你離開視圖:

- (void)viewDidDisappear:(BOOL)animated { 
    boundWidth = self.view.bounds.size.width; 
}