2015-05-04 76 views
4

我試圖在使用AirPlay的外接顯示器上顯示全屏,16:9,UIViewController從橫向iPad啓動時,UIViewController在AirPlay屏幕上橫向顯示

這裏的目標是用自定義視圖控制器替換AirPlay鏡像,該控制器將跨越整個外部屏幕的大小。

當iPad處於縱向模式時,屏幕連接時,一切似乎都很好用。當它橫向連接時,UIViewController在外部顯示器上橫向顯示,僅填滿屏幕的一半。

爲了達到此目的,我將UIViewController添加到附加的AirPlay UIScreen

-(void) screenConnected:(NSNotification *) notification { 

    UIScreen * screen = [notification object]; //this should be the airplay display 
    UIWindow * window = [[UIWindow alloc] initWithFrame:screen.bounds]; 
    [currentWindow setScreen:screen]; 
    UIViewController * controller = [_delegate createControllerForAirplayDisplay:window]; 
    [window setHidden:NO]; 
    [window setRootViewController:controller]; 

} 

這似乎工作正常,我看到在iPad上的全屏顯示以及AirPlay電視。

我在哪裏看到一個問題,就是在iPad處於橫向模式時AirPlay顯示器已連接。發生這種情況時,AirPlay顯示器會側身顯示UIViewController,看起來像肖像模式。

屏幕連接肖像:

Launched in portrait, displays ok

屏連接景觀,內容橫盤,只有一半呈現在顯示器上:

Launched in landscape, displays sideways

我已經嘗試旋轉使用UIWindowUIViewControllerCGAffineTransformMakeRotation,這會確定方向,但視圖不在AirPlay顯示內部。

編輯

提起與蘋果的問題與此相關,rdar:// 20817189。如果/當我聽到回聲,我會更新它。

+1

有趣。升級到IOS 8.3後,我再次看到同樣的問題。現在我分享你的悲哀。你有沒有收到蘋果票上的回覆? – ganta

+0

@gant仍然沒有在票上 – francis

回答

9

我發現這個文檔中的代碼實例: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/UsingExternalDisplay/UsingExternalDisplay.html

重要:確保您的應用程序正確設置狀態欄方向。由於外部顯示屏無法讀取主機設備的加速度計以響應方向的變化,因此它依賴於狀態欄方向,如同在應用程序中設置的一樣。

外部UIWindow取當前設備的方向,在這種情況下是橫向方向。 因此,當您設置rootViewController的框架時,例如(0,0,1270,840),原點位於電視機的右上角。

您必須以縱向方向強制UIWindow。 我用這段代碼解決了這個問題AppDelegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)_window { 
if ([UIScreen mainScreen] == _window.screen || !_window) { 
    return UIInterfaceOrientationMaskAll; 
}else { 
    return UIInterfaceOrientationPortrait; 
} 
} 
+0

謝謝,這是行不通的。然而,它看起來有點不直觀,我很高興你能夠解讀來自文檔的註釋。 – francis

+1

更正:你在else語句中有錯誤的Enum。它應該是:UIInterfaceOrientationMaskPortrait。謝謝,這也適用於我。接近我的答案,但誰知道你必須聲明肖像的方向,實際上是風景:(有意義,雖然如果它離開狀態欄和默認是肖像 – ganta

+0

- (UIInterfaceOrientationMask)應用程序:(UIApplication *)應用程序supportedInterfaceOrientationsForWindow:(UIWindow *)_windows委託方法返回UIInterfaceOrientationMask而不是NSUInteger。 –

0

讓你的shouldAutorotateToInterfaceOrientation方法總是返回YES應該爲你整理。

+0

這似乎並沒有解決這個問題,電視界面似乎在橫向顯示,只是旋轉。 – francis

0

設置爲縱向蒙版的Airplay窗口永遠不會旋轉。默認狀態欄是肖像,因此旋轉值基於此。這裏是一個作品(假設您已經存儲在您的Airplay的UIWindow爲self.secondWindow在你的AppDelegate當它連接)

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 
    if(window == self.secondWindow){ 
     return UIInterfaceOrientationMaskPortrait; 
    } 
    return UIInterfaceOrientationMaskAll; 
} 
相關問題