目前網上關於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