2013-09-24 73 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