2013-02-27 23 views
3

我在設備旋轉時遇到問題。除了顯示公司啓動畫面的ONE視圖外,我想將所有剩餘的應用程序視圖鎖定到肖像顯示中。在項目設置中,支持的方向是Portrait和LandscapeLeft。在'Company Splash'中,它工作正常,無論我如何旋轉設備,視圖旋轉都被鎖定在LandscapeLeft中。在所有其他視圖中,當我將設備向左旋轉時,視圖改變而不是保持縱向顯示。該方法甚至沒有解僱?如果我從項目中支持的方向中移除左側的橫向,則會將「公司飛濺」視圖關閉。我嘗試將shouldAutorotate更改爲NO,但這並沒有幫助。試圖通過here發佈的建議工作,但這並沒有幫助。如果我將下面的代碼放到我的AppDelegate.m中,所有內容都被鎖定到縱向模式,並且訪問「公司飛濺」時崩潰。Xcode - 如何讓視圖鎖定到肖像模式,但仍然允許一個視圖旋轉?

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
} 

無論設備如何旋轉(除了一個屏幕),我如何將視圖鎖定爲肖像模式?

**方法從'Company Splash'視圖。再次,它應該是這樣的作品。

-(NSInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeLeft; 
} 

從所有其他視圖,其旋轉出人像的時候,我不希望他們以

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // IOS5 Only returning that it should rotate to potrait 
    return (interfaceOrientation == UIDeviceOrientationPortrait); 
} 

-(BOOL)shouldAutorotate 
{ 
    // forcing the rotate IOS6 Only 
    return YES; 
} 

-(NSInteger)supportedInterfaceOrientations 
{ 
    // return number or enum IOS6 Only 
    return UIInterfaceOrientationMaskPortrait; 
} 

我想也許這可能是因爲一個的UITabBarController是根控制器**方法和我在ViewController中呢?該方法甚至沒有解僱?

回答

6

觀察員加入視圖的viewDidLoad方法要旋轉這樣的:

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

,然後設置根據橫向視圖內的orientationChanged方法這樣的觀點:

- (void) orientationChanged:(NSNotification *)note{ 
UIDevice * device = [UIDevice currentDevice]; 
switch(device.orientation) 
{ 
    case UIDeviceOrientationPortrait: 

     break; 
    case UIDeviceOrientationPortraitUpsideDown: 

     break; 
    case UIDeviceOrientationLandscapeLeft: 

     break; 
    case UIDeviceOrientationLandscapeRight: 

     break; 

    default: 
     break; 
    }; 
} 
+0

對不起,我花了這麼長時間迴應。我會在什麼情況下強制設備進行一定的旋轉? – Grymjack 2013-02-28 23:53:15

+0

僅讓您的應用程序僅在縱向模式下運行,並在您需要旋轉的視圖的viewDidLoad方法中添加上面提到的觀察者,然後將orientationChanged方法添加到該類中。當用戶旋轉視圖時,您將收到通知。因此,您可以通過代碼 – Ushan87 2013-03-01 05:45:49

+0

設置橫向模式的視圖並不是我遇到的問題,但此答案確實有效。 :) – Grymjack 2013-03-01 15:43:45

0

也許ü應該允許所有的方向,並在每個班級鎖定縱向除公司飛濺

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if (interfaceOrientation == UIInterfaceOrientationPortrait) { 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
    } else { 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
    } 
} 
+0

我已經嘗試過,你想(以上的鎖它在橫向)的方向並沒有產生效果。我想也許這可能是因爲UITabBarController是根控制器,並且我在ViewController中關閉了它?該方法甚至沒有解僱? – Grymjack 2013-02-27 12:03:13

+0

類似的事情發生了什麼事情。將rootViewController設置爲tabcontroller將導致這些方法僅在tabControllerClass.m中觸發,而不是在各種tabViewController.m方法中觸發。但是,現在我遇到了一個問題,試圖確定當該方法在tabcontroller級別觸發時我正在查看哪個選項卡。必須有一個更優雅的方式來處理這個問題。 – Grymjack 2013-03-01 00:22:36

2

將此方法放入要鎖定在某個方向的視圖控制器中。

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

只是改變

相關問題