UITabBarController移除系统自带UITabBarButton简单实现

以下做法在iOS 9、10 上可行

在viewWillAppear:方法移除

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    for (UIView *view in self.tabBar.subviews) {
        if([view isKindOfClass:NSClassFromString(@"UITabBarButton")])
            [view removeFromSuperview];
    }
    NSLog(@"%@",self.tabBar.subviews);
}

结果


WX20180613-211837.png

WX20180613-212032.png

在以下两个方法再次查看tabBar的子控件

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    NSLog(@"%s",__func__);
    NSLog(@"%@",self.tabBar.subviews);
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"%s",__func__);
    NSLog(@"%@",self.tabBar.subviews);
}

结果显示确实移除了UITabBarButton


WX20180613-212441.png

在iOS11上采用同样的做法

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    for (UIView *view in self.tabBar.subviews) {
        if([view isKindOfClass:NSClassFromString(@"UITabBarButton")])
            [view removeFromSuperview];
    }
    NSLog(@"%s",__func__);
    NSLog(@"%@",self.tabBar.subviews);
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    NSLog(@"%s",__func__);
    NSLog(@"%@",self.tabBar.subviews);
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"%s",__func__);
    NSLog(@"%@",self.tabBar.subviews);
}

结果
虽然在viewWillApper:方法中移除了UITabBarButton,而且在viewWillLayoutSubViews中显示为空,但是系统又做了一些处理,还原了UITabBarButton


WX20180613-212746.png

暂时解决方案

将移除子控件的业务逻辑写到viewWillLayoutSubviews方法中,得到以下结果,可以移除子控件


WX20180613-213152.png

实验测试写在viewDidAppear:、viewDidLayoutSubviews均可行

你可能感兴趣的:(UITabBarController移除系统自带UITabBarButton简单实现)