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上是有本质差别的。