目前网上关于UITabBarController 在iOS6和iOS7下隐藏TabBar 都是一下代码。ide
@interface UITabBarController (HideTabBar) @property (nonatomic, getter=isTabBarHidden) BOOL tabBarHidden; - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated; @end
#define kAnimationDuration .3 #import "UITabBarController+HideTabBar.h" @implementation UITabBarController (HideTabBar) - (BOOL)isTabBarHidden { CGRect viewFrame = self.view.frame; CGRect tabBarFrame = self.tabBar.frame; return tabBarFrame.origin.y >= viewFrame.size.height; } - (void)setTabBarFrame { CGRect tabBarFrame = self.tabBar.frame; tabBarFrame.origin.y = self.view.frame.size.height - tabBarFrame.size.height; } - (void)setTabBarHidden:(BOOL)hidden { [self setTabBarHidden:hidden animated:NO]; } - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated { BOOL isHidden = self.tabBarHidden; UIView *transitionView = [[[self.view.subviews reverseObjectEnumerator] allObjects] lastObject]; if(hidden == isHidden){ return; } if(transitionView == nil) { NSLog(@"could not get the container view!"); return; } CGRect viewFrame = self.view.frame; CGRect tabBarFrame = self.tabBar.frame; CGRect containerFrame = transitionView.frame; tabBarFrame.origin.y = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height); containerFrame.size.height = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height); tmpRect = containerFrame; [UIView animateWithDuration:kAnimationDuration animations:^{ self.tabBar.frame = tabBarFrame; transitionView.frame = containerFrame; } ]; } @end
这段代码正常是能够隐藏,可是在iOS6状况下,有时候是没法去隐藏UITabBar.atom
在此基础上,我增长了几行代码。以下:code
#define kAnimationDuration .3 #import "UITabBarController+HideTabBar.h" CGRect tmpRect; @implementation UITabBarController (HideTabBar) - (BOOL)isTabBarHidden { CGRect viewFrame = self.view.frame; CGRect tabBarFrame = self.tabBar.frame; return tabBarFrame.origin.y >= viewFrame.size.height; } - (void)setTabBarFrame { CGRect tabBarFrame = self.tabBar.frame; tabBarFrame.origin.y = self.view.frame.size.height - tabBarFrame.size.height; } - (void)setTabBarHidden:(BOOL)hidden { [self setTabBarHidden:hidden animated:NO]; } - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated { BOOL isHidden = self.tabBarHidden; UIView *transitionView = [[[self.view.subviews reverseObjectEnumerator] allObjects] lastObject]; if(hidden == isHidden){ transitionView.frame = tmpRect; return; } if(transitionView == nil) { NSLog(@"could not get the container view!"); return; } CGRect viewFrame = self.view.frame; CGRect tabBarFrame = self.tabBar.frame; CGRect containerFrame = transitionView.frame; tabBarFrame.origin.y = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height); containerFrame.size.height = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height); tmpRect = containerFrame; [UIView animateWithDuration:kAnimationDuration animations:^{ self.tabBar.frame = tabBarFrame; transitionView.frame = containerFrame; } ]; } @end
增长了一个get
CGRect tmpRect;
这样在iOS6状况下,就能够直接隐藏UITabBar.animation