標籤:
iOS自訂tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar 的解決辦法
問題:iOS自訂tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar
1.自訂代碼:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 刪除系統自動產生的UITabBarButton
[self removeTabBarButton];
}
-(void) removeTabBarButton {
// 刪除系統自動產生的UITabBarButton
for (UIView *child in self.tabBar.subviews) {
if ([child isKindOfClass:[UIControl class]]) {
[child removeFromSuperview];
}
}
}
/**
* 初始化tabbar
*/
- (void)setupTabbar
{
HYTTabBar *customTabBar = [[HYTTabBar alloc] init];
customTabBar.frame = self.tabBar.bounds;
customTabBar.delegate = self;
[self.tabBar addSubview:customTabBar];
self.customTabBar = customTabBar;
}
2.pop代碼:
[self.navigationController popToViewController:strongSelf.navigationController.childViewControllers[1] animated:YES];
3.結果:
解決方案:
1. pop的時候 發送通知(注意是從 要pop回帶有tabber的那個VC控制器發出通知)
NSNotification *notification =[NSNotification notificationWithName:@"HYTPopViewControllerNotification" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:notification];
2. 在自訂的tabcontroller 的viewdidload方法中註冊通知,調用removeTabBarButton方法刪除系統內建的就可以了
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化tabbar
[self setupTabbar];
//.../
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HYTPopViewControllerNotification) name:@"HYTPopViewControllerNotification" object:nil];
}
-(void) HYTPopViewControllerNotification {
// 刪除系統自動產生的UITabBarButton
for (UIView *child in self.tabBar.subviews) {
if ([child isKindOfClass:[UIControl class]]) {
[child removeFromSuperview];
}
}
}
ps:我嘗試過連續調用幾個popviewcontroller的方法來替代poptoviewcontroller,結果正常。
這說明popviewcontroller 和 poptoviewcontroller 的實現至少在自訂tabbar上是有本質差別的。
iOS自訂tabbar後popToRootViewContriller和poptoviewcontroller時出現兩個tabbar 的解決辦法