2012-08-03 41 views
1

我有一個UIPageViewController,其中視圖中添加了兩個子視圖:帶有UITapGestureRecognizer的透明子視圖和帶有一些按鈕的工具欄,當我點擊另一個子視圖時,該按鈕從底部向上滑動。UIPageViewController在旋轉之後將子視圖帶到前面

編輯。 這是子視圖設置在viewDidAppear:(BOOL)動畫

CGRect frame; 
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice]orientation])){ 
    frame = CGRectMake(50, 0, 220, 480); 
} 
else { 
    frame = CGRectMake(50, 0, 380, 320); 
} 
if (!tapView) { 
    tapView = [[LSTapView alloc]initWithFrame:frame]; 
    //[tapView setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.4]]; 
    [self.view addSubview:tapView]; 
    [tapView release]; 
} 
else { 
    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice]orientation])){ 
     [tapView setFrame:CGRectMake(50, 0, 220, 480)]; 
     [fontViewController.view setFrame:CGRectMake(0, 480, 320, 92)]; 
    } 
    else { 
     [tapView setFrame:CGRectMake(50, 0, 380, 320)]; 
     [fontViewController.view setFrame:CGRectMake(0, 320, 480, 92)]; 

    } 
} 

if (!fontViewController){ 
    fontViewController = [[LSFontViewController alloc]initWithNibName:@"LSFontView" bundle:nil]; 
} 
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice]orientation])){ 
    [fontViewController.view setFrame:CGRectMake(0, 480, 320, 92)]; 
} 
else { 
    [fontViewController.view setFrame:CGRectMake(0, 320, 480, 92)]; 
} 

[self.view addSubview:fontViewController.view]; 

如果我改變頁面,而無需旋轉設備,一切都運行在兩個方向的罰款。然而,當我旋轉設備時,這兩個子視圖消失了,我發現它們不在前面。無論如何,如果我加入這個我的代碼:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    [self.view bringSubviewToFront:tapView]; 
    [self.view bringSubviewToFront:fontViewController.view]; 
} 

一個奇怪的現象發生了:我不能改變的網頁了,或者更好的,一個和下一個viewControllers正確加載,但他們不顯示和網頁別改變。

有人可以解釋我發生了什麼?

謝謝。 L.

+0

你能告訴你如何最初設置你的子視圖? – sergio 2012-08-03 17:16:09

+0

當然,我會編輯問題.. – Lolloz89 2012-08-04 09:18:27

回答

1

我通過註冊UIPageViewController到UIDeviceOrientationDidChangeNotification這樣解決了這個問題,簡單地添加了didRotateFromInterfaceOrientation選擇器,將數據源搞亂並導致奇怪的崩潰。

0

您的問題應該與在iOS 5下旋轉設備後不自動調用viewWillAppear這一事實有關。因此,當您旋轉設備時,您將代碼重新排列子視圖的框架不是執行。我不知道這是iOS 5中的錯誤,還是iOS 5的某些特定小版本中的錯誤,但是幾周前我發現了這個錯誤,並且它在自動化存在的情況下也破壞了我的邏輯。

一個簡單的嘗試修復,這將是:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    [self viewWillAppear:NO]; 
} 

讓我知道這是否正常工作。

在任何情況下,我覺得很困惑,你正在viewWillAppear實例化你的意見。國際海事組織,正確的地方是viewDidLoad

我的建議是處理子視圖創建的邏輯在viewDidLoad然後處理視圖定位的邏輯在一個單獨的方法,我們稱之爲layoutSubviews,你會從viewDidLoaddidRotateFromInterfaceOrientation同時調用。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil]; 

,並通過添加這個簡單的選擇:

-(void)didChangeOrientation{ 
    [self.view bringSubviewToFront:tapView]; 
    [self.view bringSubviewToFront:fontViewController.view]; 
} 

對於一些原因,我真的不知道和了解

+0

感謝您的回答,但不幸的是,這並沒有幫助... – Lolloz89 2012-08-04 10:10:20

+0

你可以檢查viewWillAppear執行時,你旋轉設備,並通過它來確保一切都發生在你身邊期望?還請檢查我的建議以上... – sergio 2012-08-04 11:02:46

+0

一件重要的事情:在viewWillAppear,確保設備的方向是你期望的... – sergio 2012-08-04 11:06:35