iOS&Swift自定义的tabbar出现重叠

(使用popToViewController导致的UITabBarButton重叠的问题)

在iOS8 中允许动态添加tabbaritem,自定义的tabbar会出现重叠的情况,原本viewDidLoad()中已经移除的UITabBarButton再次出现。

解决:在自定义的BaseViewController的viewWillLayoutSubViews()方法里去移除UITabBarButton

// MARK: - Swift移除UITabBarButton
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        //print("viewWillLayoutSubviews:\(self.tabBarController?.tabBar.subviews)")
        let aClass:AnyClass = NSClassFromString("UITabBarButton")!
        guard let views = self.tabBarController?.tabBar.subviews else { return }
        for view in views {
            if view.isKind(of: aClass) {
                view.removeFromSuperview()
            }
        }
    }


#pragma mark - OC移除UITabBarButton
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    // tabBar移除UITabBarButton
    for (UIView *subView in self.tabBar.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [subView removeFromSuperview];
        }
    }
}




猜你喜欢

转载自blog.csdn.net/wenzfcsdn/article/details/78933586