2015-09-14 67 views
0

我有消息應用程序例如WhatsApp & ...... 這樣的層次:防止改變方向的視圖 - 控制

Tab Bar Controller 
    Navigation Controller 
     View Controller 
      Target Controller 

,所以我們打算從的appDelegate

UITabBarController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"mainTab"]; 
self.window.rootViewController = ivc; 
[self.window makeKeyAndVisible]; 

的TabBar這樣我的問題 我想在某個條件下鎖定方向目標控制器-(BOOL)shouldAutorotatesupportedInterfaceOrientations不叫,我已閱讀了一些關於這個帖子,但我找不到正確的解決方案。 的帖子是這樣的:多小時搜索 Lock Screen Rotation in iOS 8

Handling autorotation for one view controller in iOS7

UPDATE

後,我找到了解決辦法,但還不夠 此鏈接暗示對我來說,如果我們使用UITabController每事情會改變,所以我們應該打電話

-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController 
{ 
return navigationController.topViewController.supportedInterfaceOrientations; 
    } 

-(NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController 
    { 
return ((parentOfTarget*)[tabBarController.viewControllers objectAtIndex:0]).supportedInterfaceOrientations; 
} 

這個原因我通知改變方向&阻止它,但我會看到狀態欄的方向。

我想我需要調用

navigationController.topViewController.shouldAutorotate; 

但它是導致死機;(

TabBarController: Orienting views in different orientations

回答

0

使用此方法停止橫向

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

    // Use this to allow upside down as well 
    //return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); 
} 

,如果你想使用您可以在appdelegate.m中使用的shouldAutorotate委託方法。

- (BOOL)shouldAutorotate { 
    return NO; 
} 

如果您正在使用最新的iOS,然後使用這些方法: -

- (NSUInteger) supportedInterfaceOrientations { 
    // Return a bitmask of supported orientations. If you need more, 
    // use bitwise or (see the commented return). 
    return UIInterfaceOrientationMaskPortrait; 
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
} 

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation { 
    // Return the orientation you'd prefer - this is what it launches to. The 
    // user can still rotate. You don't have to implement this method, in which 
    // case it launches in the current orientation 
    return UIInterfaceOrientationPortrait; 
} 
+0

它棄用&也在我的視圖控制器 – Mohamad

+0

不叫我知道它的過時,但它的工作......好吧,你可以看到我的更新,回答這個查詢 –

+0

沒有工作,它與UINavigationController的 – Mohamad

1

您應該使用-supportedInterfaceOrientations' instead of -shouldAutorotate`。

以後只能在運行時決定方向時使用。在你的情況下,你的視圖控制器將始終只支持肖像模式。

接下來,您的導航控制器的委託必須實現以下方法,以在導航堆棧頂部的視圖控制器上返回調用-supportedInterfaceOrientations的結果。

-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController { 
    return navigationController.topViewController.supportedInterfaceOrientations; 
} 
+0

我造成的已經調用supportedInterfaceOrientations但他們沒有在我的ViewController中調用,我會嘗試你的提示。(我應該在哪裏實施這個代表,在TagetViewController? – Mohamad

+0

任何方向的代表不是我的'viewController',只有'wilRotate'調用&'didRoate'叫我'ViewControoler',我真的搞不清楚這一點。 – Mohamad

+0

@please再次檢查 – Mohamad