977
技術社區[雲棲]
IOS中如何在多層界麵之間顯示與隱藏標簽欄(UITabBar)
在做項目的時候,遇到了一個難題,使用hidesBottomWhenPushed=YES屬性設置,可以讓本級界麵及其以後界麵都隱藏,但是根據項目
需求,在第4層是需要顯示標簽欄的。
於是想了很多的辦法,每個要顯示的push的界麵前都加了hidesBottomWhenPushed=YES,要隱藏的push的界麵都加了hidesBottomWhenPushed=NO,
但是一樣不好使。
手動調用self.tabbarchontroller.tabbar.hiden = YES,這樣寫可以隱藏標簽欄的項,但是上麵有一層白色的空白視圖占著,一樣沒有解決問題。
最後想到了修改標簽欄的Frame的方法來解決,果真能行。
我把下麵的代碼寫到了項目中所有視圖控製器的父類中:(記得暴露出這兩個方法,讓子類調用)
#pragma mark - 隱藏TabbBar
- (void)hideTabbar {
if (_originY + 49 == fabs(self.tabBarController.tabBar.frame.origin.y)) {
return ;
}
for (UIView *v in [self.tabBarController.view subviews]) {
if ([v isKindOfClass:[UITabBar class]]) {
[UIView animateWithDuration:0.01 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(){
CGRect frame = v.frame;
frame.origin.y += 49.0f;
v.frame = frame;
NSLog(@"tabBar originY: %f", frame.origin.y);
} completion:nil];
} else {
[UIView animateWithDuration:0.01 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(){
CGRect frame = v.frame;
frame.size.height += 49.0f;
v.frame = frame;
} completion:nil];
}
}
return;
}
#pragma mark - 顯示TabBar
- (void)showTabBar {
if (_originY == fabs(self.tabBarController.tabBar.frame.origin.y)) {
return ;
}
for (UIView *v in [self.tabBarController.view subviews]) {
if ([v isKindOfClass:[UITabBar class]]) {
[UIView animateWithDuration:0.01 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(){
CGRect frame = v.frame;
frame.origin.y -= 49.0f;
v.frame = frame;
} completion:nil];
} else {
[UIView animateWithDuration:0.01 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(){
CGRect frame = v.frame;
frame.size.height -= 49.0f;
v.frame = frame;
} completion:nil];
}
}
return;
}
最後更新:2017-04-03 12:56:11