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

你可能感兴趣的:(iOS 使用自定义UITabBarController或者TabBar调用popTo或pop后底部TabBar重叠问题)