iOS 使用自定义UITabBarController或者TabBar调用popTo或pop后底部TabBar重叠问题

其实重叠的原因是系统添加了自带的UITabBarButton,将其删除即可
解决方法一:
在自定义的UITabBarController中添加以下代码

- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
  for (UIView *subView in self.tabBar.subviews) {
      // 删除系统自带的tabBarButton
      ![subView isKindOfClass:NSClassFromString(@"UITabBarButton")] ?: [subView removeFromSuperview];
  }
}

解决方法二:

在自定义的NavigationController中遵循UINavigationController的代理
设置代理:

- (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];
        }
    }
}

解决方法二:
在自定义的NavigationController中添加如下代码:

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

- (void)removeTabBarBtn {
    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];
}

并在调用popTo或pop方法的viewController中发出通知:

[self.navigationController popToRootViewControllerAnimated:YES];
/// 或者
///[self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:@"removeTabBarBtn" object:nil userInfo:nil];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容