以下做法在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);
}
结果
在以下两个方法再次查看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
在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
暂时解决方案
将移除子控件的业务逻辑写到viewWillLayoutSubviews方法中,得到以下结果,可以移除子控件
实验测试写在viewDidAppear:、viewDidLayoutSubviews均可行