2012-06-13 123 views
0

所以我的iPhone應用程序目前有一個tabviewcontroller來填充整個屏幕。該應用只能在肖像模式下運行。我的任務是檢測設備方向的變化,一旦它改變爲橫向,一個新的uiview填充整個屏幕。在設備方向從縱向變爲縱向時使用新的uiview填充整個橫向屏幕

我已經有設備方向更改檢測工作。一旦檢測到方向更改,我已經使用NSNotificationCenter成功調用輔助方法deviceOrientationChanged。如果變化是橫向模式,我運行一段代碼。

在這段代碼中,我已經嘗試了各種各樣的東西,但都沒有成功。簡單地說self.view = newViewThing;不起作用,因爲狀態欄仍然存在於頂部,並且標籤仍然存在於底部。 我也嘗試添加這個newViewThing作爲UIWindow的子視圖。這不起作用,因爲在添加視圖時,它的方向不正確。

問題是:有沒有辦法加載一個全新的uiview一旦檢測到設備方向變化?先謝謝你。

+0

這聽起來像你AREN不會替換tabviewcontroller,只是改變它的子視圖。 「自我」指的是什麼?你能分享你的代碼嗎? – mikeyq6

回答

1

是的,有一種方法來加載一個新的視圖。我讓我的應用程序那樣:

- (void)orientationChanged:(NSNotification *)notification 
{ 
    // We must add a delay here, otherwise we'll swap in the new view 
    // too quickly and we'll get an animation glitch 
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0]; 
} 

- (void)updateLandscapeView 
{ 
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView) 
    { 
     [self presentModalViewController:self.landscapeView animated:YES]; 
     isShowingLandscapeView = YES; 
    } 
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView) 
    { 
     [self dismissModalViewControllerAnimated:YES]; 
     isShowingLandscapeView = NO; 
    }  
} 

而且也是我加入這個代碼viewDidLoad

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) 
              name:UIDeviceOrientationDidChangeNotification object:nil]; 

與此代碼dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
+0

感謝Maxim爲您提供解決方案。這是一個功能性解決方案!然而,從縱向到橫向的過渡有點粗糙,因爲在方向改變時,應用程序首先旋轉已經存在的第一個視圖,然後第二個視圖彈出它的位置。 _是否有任何方法讓過渡變得不那麼笨拙?在中,是否有辦法在第二個視圖彈出之前不旋轉第一個視圖,或者類似的東西?_ –

相關問題