2014-02-17 44 views
0

由於問題導致我無法在主要的UIWindow中顯示內容,因此我在應用程序中添加了第二個UIWindow。我知道這不是建議的,但我有點被迫走這條路。第二個UIWindow中的方向更改

無論如何,我創建了第二個UIWindow,但我現在試圖設置它以響應方向更改,因爲它不會自動執行此操作,與主窗口不同。問題是我對iOS特定編碼方面的經驗不多,而且我對此有點不知所措。

這是我這麼遠:

// Create the new UIWindow 
mWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, size.height, size.width)]; 
mWindow.windowLevel = UIWindowLevelStatusBar + 1.0f; 
[mP3NMoreGamesWindow setRootViewController:mMyViewController]; 

MyViewController定位特定的功能:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations. 
    NSString * supported_orientation = [Utilities GetDeviceOrientation]; 
    if ([supported_orientation isEqualToString:@"landscape"] && (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)) 
    { 
     return YES; 
    } 
    else if ([supported_orientation isEqualToString:@"portrait"] && (interfaceOrientation == UIInterfaceOrientationPortrait || 
                    interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
    { 
     return YES; 
    } 

    return NO; 
} 

- (BOOL)shouldAutorotate 
{ 
    //returns true if want to allow orientation change 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    NSString * supported_orientation = [Utilities GetDeviceOrientation]; 
    if ([supported_orientation isEqualToString:@"landscape"]) 
    { 
     return UIInterfaceOrientationMaskLandscape; 
    } 
    else 
    { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

有什麼我失蹤?我的印象是,窗口的rootviewcontroller收到方向更改通知?

另外,是否可以在創建時設置UIWindow的方向?例如,如果當我創建UIWindow時當前的方向是風景,我可以將UIWindowfi設置爲橫向,而不是縱向默認的縱向?

謝謝。

回答

0

好吧,我不確定你到底想要完成什麼,所以我可能會脫離基地。然而,看看你的代表方法:

- (BOOL)shouldAutorotateToInterfaceOrientation: 

(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations. 
    NSString * supported_orientation = [Utilities GetDeviceOrientation]; 
    if ([supported_orientation isEqualToString:@"landscape"] && (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)) 
    { 
     return YES; 
    } 
    else if ([supported_orientation isEqualToString:@"portrait"] && (interfaceOrientation == UIInterfaceOrientationPortrait || 
                    interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
    { 
     return YES; 
    } 

    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    NSString * supported_orientation = [Utilities GetDeviceOrientation]; 
    if ([supported_orientation isEqualToString:@"landscape"]) 
    { 
     return UIInterfaceOrientationMaskLandscape; 
    } 
    else 
    { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

確保他們實際上被調用一些斷點。我的猜測是他們不是。

看看這個:http://shaune.com.au/using-multiple-uiwindows-in-ios-applications/

鑑於所有這一切,當然最好將使用NSNotificationCenter通知你的第二個窗口時第一個會轉動和處理手動轉動而設置的通知。

+0

檢查並旋轉後調用這些函數。我認爲實際的問題是新UIWindow在設置時沒有考慮當前的方向。例如,創建第二個UIWindow時,該應用處於橫向模式,但創建的UIWindow隱式設置爲縱向。是否可以在創建時直接設置窗口的方向? – Benzino

1

該解決方案位於應用程序委託中。您可以單獨處理每個窗口的支持的方向:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    // check, which window is asking and return the mask 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 
相關問題