iOS之隐藏/显示tabbar

本文介绍了一种在Swift中通过调整UIView的frame来间接隐藏TabBar的方法,避免了使用直接隐藏属性导致的显示问题。这种方法可以有效防止界面上出现黑色区域,并保持应用界面的一致性和美观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    func setTabBarHidden(flag:Bool!){
        //将view的frame放大到全屏,就把TabBar变相隐藏了,不会出现黑色区域。
        if UIApplication.sharedApplication().keyWindow?.rootViewController?.isKindOfClass(UITabBarController.self) == true{
            let tabVC : UITabBarController = UIApplication.sharedApplication().keyWindow?.rootViewController as! UITabBarController
           // if tabVC.tabBar.hidden != flag{
                debugPrint("UITabBarController hidden \(tabVC.tabBar.hidden)")

                let tab = tabVC.view//self.tabBarController?.view
                if tab?.subviews.count < 2 {
                    return
                }
                var tmpView : UIView!
                if tab?.subviews[0].isKindOfClass(UITabBar.self) == true {
                    tmpView = tab?.subviews[1]
                }else{
                    tmpView = tab?.subviews[0]
                }
                if flag == true{
                    tmpView.frame = (tab?.bounds)!
                    tmpView.frame = CGRectMake(tmpView.bounds.origin.x,  tmpView.bounds.origin.y,  tmpView.bounds.size.width, tmpView.bounds.size.height + self.tabBarController!.tabBar.frame.size.height);

                }else{
                    tmpView.frame = CGRectMake((tab?.bounds.origin.x)!, (tab?.bounds.origin.y)!, (tab?.bounds.size.width)!, (tab?.bounds.size.height)!)

                }
                debugPrint("UITabBarController tmpView \(tmpView)")
//                tabVC.view.backgroundColor = UIColor.whiteColor()
                tabVC.tabBar.hidden = flag
            }
        //}

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值