iOS自定义TabBar使用popToRootViewControllerAnimated重叠问题解决

先看下现象吧,如下图:


不仅仅重叠了,而且还把图标给覆盖了。尴尬很郁闷。

折腾了很久,终于找到解决办法了。

解决方法一:

在自定也的NavigationController中添加如下代码:

- (void)viewDidLoad{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeTabBarBtn) name:@"removeTabBarBtn" object:nil];
}

- (void)removeTabBarBtn
{
//    NSArray *tSubviews = self.tabBarController.tabBar.subviews;
//    for (int i = 0; i < tSubviews.count; i++) {
//        Class parentVCClass = [tSubviews[i] class];
//        NSString *className = NSStringFromClass(parentVCClass);
//        ALog(@"%d---%@",i, className);
//    
//    }
    
    for (UIView *tabBar in self.tabBarController.tabBar.subviews) {
        if ([tabBar isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBar removeFromSuperview];
        }
    }
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"removeTabBarBtn" object:nil];
}

并且在调用popToRootViewControllerAnimated方法的viewController中发出通知:

    [self.navigationController popToRootViewControllerAnimated:NO];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"removeTabBarBtn" object:nil userInfo:nil];



解决方法二:

(该方法更为简单)苹果强大就强大在这里,他们已经预想到了。

所以方法就是:遵循UINavigationController的代理,用代理方法解决该Bug,代码如下:


设置代理:

- (void)viewDidLoad{
    [super viewDidLoad];
    self.delegate = self;
}

实现代理方法:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 删除系统自带的tabBarButton
    for (UIView *tabBar in self.tabBarController.tabBar.subviews) {
        if ([tabBar isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBar removeFromSuperview];
        }
    }
}


最后运行,完美解决。用王祖蓝常说的一句话:完美 !

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值