2013-09-24 222 views
0

我有一個標籤式應用程序,在一個標籤中有一個UIWebView。當我將設備旋轉到橫向時,我想將此UIWebView全屏顯示在狀態欄和選項卡欄上。iOS隱藏iOS 6 + 7中的狀態欄和標籤欄

//編輯

好了,現在我已經得到了它在iOS 6中的工作 - 原本旋轉和隱藏的標籤欄會留下一個黑色的空間,標籤欄是時候,所以fHeight代碼修復這個。然而,在iOS 6上它完美運行,但現在它實際上創建了iOS 6的黑條問題!任何想法解決這個問題?

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; 
{ 
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     [self hideTabBar:self.tabBarController]; 
     [[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationSlide]; 
    } 
    else 
    { 
     [self showTabBar:self.tabBarController]; 
     [[UIApplication sharedApplication] setStatusBarHidden:FALSE withAnimation:UIStatusBarAnimationSlide]; 
    } 
} 

- (void) hideTabBar:(UITabBarController *) tabbarcontroller 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    float fHeight = screenRect.size.height; 
    if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     fHeight = screenRect.size.width; 
    } 

    for(UIView *view in self.tabBarController.view.subviews) 
    { 
     if([view isKindOfClass:[UITabBar class]]) 
     { 
      [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)]; 
     } 
     else 
     { 
      [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)]; 
      view.backgroundColor = [UIColor blackColor]; 
     } 
    } 
    [UIView commitAnimations]; 
} 

- (void) showTabBar:(UITabBarController *) tabbarcontroller 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    float fHeight = screenRect.size.height - 49.0; 

    if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     fHeight = screenRect.size.width - 49.0; 
    } 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    for(UIView *view in tabbarcontroller.view.subviews) 
    { 
     if([view isKindOfClass:[UITabBar class]]) 
     { 
      [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)]; 
     } 
     else 
     { 
      [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)]; 
     } 
    } 
    [UIView commitAnimations]; 
} 

//編輯2

我已經使用這個嘗試,但我不知道我需要傳遞什麼看法?它應該適用於iOS 6和7

- (void)setTabBarHidden:(BOOL)hidden view:(UIView *)view animated:(BOOL)animated 
{ 
    if (self.tabBar.hidden == hidden) 
     return; 

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    float height = 0.0f; 

    if(UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     height = screenRect.size.width; 
    } 
    else 
    { 
     height = screenRect.size.height; 
    } 

    if (!hidden) 
    { 
     height -= CGRectGetHeight(self.tabBar.frame); 
    } 

    void (^workerBlock)() = ^() { 

     self.tabBar.frame = CGRectMake(CGRectGetMinX(self.tabBar.frame), height, CGRectGetWidth(self.tabBar.frame), CGRectGetHeight(self.tabBar.frame)); 
     view.frame = CGRectMake(CGRectGetMinX(view.frame), CGRectGetMinY(view.frame), CGRectGetWidth(view.frame), height); 
    }; 

    void (^completionBlock)(BOOL finished) = ^(BOOL finished) { 
     self.tabBar.hidden = hidden; 
    }; 

    if (animated) 
    { 
     [UIView animateWithDuration:0.25f animations:workerBlock completion:completionBlock]; 
    } 
    else 
    { 
     workerBlock(); 
     completionBlock(YES); 
    } 
} 
+1

爲了您自己的幸福,不要使用'[UIView beginAnimations ...]'和'[UIView commitAnimations]',而是使用基於塊的動畫方法。 –

回答

1

是的,使用適當的UIViewController旋轉方法。隱藏標籤欄控制器很容易,但iOS 7上的狀態欄更加困難。研究如何做到這一點,您應該沒問題。

+0

請參閱更新 –

+1

在您的更新中,您正在響應設備方向更改通知。這是沒有必要的。你有一個視圖控制器,爲你提供確切的方法。我會建議在'willAnimateRotationToInterfaceOrientation:duration:' –

+0

運行你的代碼好吧,我會給它一個。我是否正確地隱藏標籤欄?它不起作用..當然,它不會隱藏iOS 7中的狀態欄 - 僅iOS 6和更高版本 –

1

嘗試...

-(void)viewWillAppear:(BOOL)animated 
{ 
    [self.navigationController setNavigationBarHidden:YES animated:animated]; 
    [self setHidesBottomBarWhenPushed:YES]; 
    [super viewWillApper:animated]; 
} 

-(void)viewWillDisappear:(BOOL)animated 
{ 
    [self.navigationController setNavigationBarHidden:NO animated:animated]; 
    [self setHidesBottomBarWhenPushed:NO]; 
    [super viewWillDisapper:animated]; 
} 
+0

這將只是設置它所有的時間..而不是當它在橫向方向。請參閱我的編輯 –

1

我得到這個工作前一段時間,忘了後我答案,因爲我有兩個類似的問題去!適用於iOS 6和7

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 

    BOOL toLandscape = UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation); 

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 

    void (^workerBlock)() = ^() { 


     [[UIApplication sharedApplication] setStatusBarHidden:toLandscape withAnimation:UIStatusBarAnimationSlide]; 

     float height = toLandscape ? screenRect.size.width : screenRect.size.height - CGRectGetHeight(self.tabBarController.tabBar.frame); 

     float width = toLandscape ? screenRect.size.height : screenRect.size.width; 

     webView.frame = CGRectMake(CGRectGetMinX(webView.frame), 
           CGRectGetMinY(webView.frame), 
           width, 
           height); 

     [self moveTabBarToPosition:height]; 
    }; 

    [UIView animateWithDuration:0.25f animations:workerBlock]; 
} 
//Moving the tab bar and its subviews offscreen so that top is at position y 
-(void)moveTabBarToPosition:(int)y { 
    self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height); 

    for(UIView *view in self.tabBarController.view.subviews) { 
     if ([view isKindOfClass:[UITabBar class]]) { 
      [view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)]; 
     } else { 
      [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)]; 
      view.backgroundColor = [UIColor blackColor]; 
     } 
    } 
} 

在我的情況下,這是我的web視圖,但理論上你可以給它任何視圖。適用於iOS 6和7