隐藏的TabBar有时会在其中显示黑色视图

时间:2018-05-08 11:16:56

标签: ios swift swift3 uitabbarcontroller

我的VC有一个tabBarController,我想滚动浏览tableView来隐藏/显示取决于滚动。

我已经实现了以下方法:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
        //scrolling down
        changeTabBar(hidden: true, animated: true)
    }
    else{
        //scrolling up
        changeTabBar(hidden: false, animated: true)
    }
}

func changeTabBar(hidden:Bool, animated: Bool){
    let tabBar = self.tabBarController?.tabBar
    let offset = (hidden ? UIScreen.main.bounds.size.height : UIScreen.main.bounds.size.height - (tabBar?.frame.size.height)! )
    if offset == tabBar?.frame.origin.y {return}

    self.mainCollectionView.frame.size.height = self.mainCollectionView.frame.size.height + (tabBarController?.tabBar.frame.size.height)!

    let duration:TimeInterval = (animated ? 0.5 : 0.0)
    UIView.animate(withDuration: duration,
                   animations: {tabBar!.frame.origin.y = offset},
                   completion:nil)
}

但我认为这与self.mainCollectionView.frame.size.height = self.mainCollectionView.frame.size.height + (tabBarController?.tabBar.frame.size.height)!有关。在该功能中调用它。我不确定。有时它确实有效,有时它是一个黑色的tabBar,它也搞乱了我的滚动。有时候我不能再滚动..但如果我发表评论它的工作正常

enter image description here enter image description here

3 个答案:

答案 0 :(得分:0)

我创建了以下方法并且在每种情况下都能正常工作

在XCODE中测试的编辑

将它放在UITabbarController的子类

var isTabBarHidden:Bool = false
func setTabBarHidden(_ tabBarHidden: Bool, animated: Bool,completion:(() -> Void)? = nil) {
    if tabBarHidden == isTabBarHidden   {

        self.view.setNeedsDisplay()
        self.view.layoutIfNeeded()

        //check tab bar is visible and view and window height is same then it should be 49 + window Heigth

        if (tabBarHidden == true && UIScreen.main.bounds.height == self.view.frame.height) {
            let offset = self.tabBar.frame.size.height
            self.view.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height + offset)

        }

        if let block = completion {

            block()
        }
        return
    }
    isTabBarHidden = tabBarHidden

    let offset: CGFloat? = tabBarHidden ? self.tabBar.frame.size.height : -self.tabBar.frame.size.height
    UIView.animate(withDuration: animated ? 0.250 : 0.0, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.curveEaseIn, .layoutSubviews], animations: {() -> Void in
        self.tabBar.center = CGPoint(x: CGFloat(self.tabBar.center.x), y: CGFloat(self.tabBar.center.y + offset!))

        //Check if View is already at bottom so we don't want to move view more up (it will show black screen on bottom ) Scnario : When  present mail app
        if (Int(offset!) <= 0 && UIScreen.main.bounds.height ==   self.view.frame.height) == false {
            self.view.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height + offset!)
        }
        self.view.setNeedsDisplay()
        self.view.layoutIfNeeded()

    }, completion: { _ in
        if let block = completion {
            block()
        }
    })
}

并从您的ViewController

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
            //scrolling down
            (self.tabBarController as! YourTabbarControllerClassName).setTabBarHidden(true, animated: true)
        }
        else{
            //scrolling up
            (self.tabBarController as! YourTabbarControllerClassName).setTabBarHidden(false, animated: true)
        }
    }

希望它有用

答案 1 :(得分:0)

hidesBottomBarWhenPushed设置为您的prepare for segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.hidesBottomBarWhenPushed = true
}

答案 2 :(得分:-1)

@Mohamed Lee 

**scrollViewWillBeginDragging()** and **scrollViewWillEndDragging()** func to show & hide tableView 

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate)
    {
        if (isScrollingStart)
        {
            isScrollingStart=NO;
            [self scrollingStopped];
        }
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    if (isScrollingStart)
    {
        isScrollingStart=NO;
        [self scrollingStopped];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    isScrollingStart=YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    isScrollingStart=YES;
}
-(void)scrollingStopped
{
    NSLog(@"Scrolling stopped");
}