【问题标题】:TabBar hidden sometimes shows a black view in it's placeTabBar hidden 有时会在其位置显示黑色视图
【发布时间】:2018-10-18 07:52:13
【问题描述】:

我的 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 并且它也弄乱了我的滚动。有时我无法再滚动了.. 但如果我评论它,它工作正常

【问题讨论】:

  • 尝试不使用动画
  • 没有解决我的问题..我试过

标签: ios objective-c swift swift3 uitabbarcontroller


【解决方案1】:

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

编辑在 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)
        }
    }

希望对你有帮助

【讨论】:

  • 你在哪里称呼它?在 scrollViewDidScroll 中?
  • @MohamedLee 在我的情况下,一些视图控制器需要隐藏标签栏,而对于一些它需要可见,所以我会从视图中调用
  • @MohamedLee 你试过这个吗? ?
  • 是的,我做到了,但它没有按我的意愿工作..我把它放在 scrollviewDidScroll 中,它根本不起作用
  • @MohamedLee 你用了这个方法还是黑屏?
【解决方案2】:
@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");
}

【讨论】:

  • 尝试了您的解决方案,但没有解决我的问题。它具有相同的行为。有时它只是将其隐藏 1 秒 .. 根本不工作。谢谢
  • @Mohamed Lee 请检查更新后的编辑解决方案
  • 不是我。那是肯定的。
【解决方案3】:

将hidesBottomBarWhenPushed 设置为您的prepare for segue

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

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多