2

我有4個標籤欄項的UITabBarController和每個標籤欄項目有一個UINavigationController。停用旋轉一個UITabbar項目

我需要一個uitabbar項目的方向鎖定只肖像。所以,我實現了下面的代碼:

創建一個自定義標籤欄控制器,並添加在它下面的代碼:

MainTabBarController.m

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // You do not need this method if you are not supporting earlier iOS Versions 
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    if (self.selectedViewController) 
     return [self.selectedViewController supportedInterfaceOrientations]; 

    return UIInterfaceOrientationMaskPortrait; 
} 

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

創建自定義導航控制器中的一個使用uitabbaritems並添加在它下面的代碼:

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return [self.topViewController supportedInterfaceOrientations]; 
} 

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

和用於在自定義導航控制器的UIViewController我加入FO下面的代碼:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

上面的代碼工作正常。我的問題是,如果你去的TabBar項目(其方向被鎖定到寫真)時,該設備已經在風景模式的方向改變爲橫向。任何人都可以請幫助我如何解決我的問題。

感謝, 阿南德。

回答

2

僅供參考! 我找到了解決問題的方法。我向viewcontroller添加了以下功能,我只想要在protrait模式下顯示:

-(void)viewWillLayoutSubviews 
{ 
    [super viewWillLayoutSubviews]; 
    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) 
    { 
     if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) 
     { 
      int orientationPortrait = UIInterfaceOrientationPortrait; 
      NSMethodSignature *sig = [[UIDevice currentDevice] methodSignatureForSelector:@selector(setOrientation:)]; 
      NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig]; 
      [invo setTarget:[UIDevice currentDevice]]; 
      [invo setSelector:@selector(setOrientation:)]; 
      [invo setArgument:&orientationPortrait atIndex:2]; 
      [invo invoke]; 
     } 
    } 
} 
+1

我看到有一個小動畫時,從肖像更改爲風景。有沒有辦法將這個動畫設置爲NO? –

+0

@justME抱歉,我不知道如何將該動畫設置爲NO。如果你發現,請讓我知道。 –

+0

我已經更新了你的代碼 –