首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >隐藏的TabBar有时会在其所在的位置显示黑色视图

隐藏的TabBar有时会在其所在的位置显示黑色视图
EN

Stack Overflow用户
提问于 2018-05-08 19:16:56
回答 3查看 715关注 0票数 0

我的VC有一个tabBarController,当我在tableView中滚动时,我想要根据滚动来隐藏/显示。

我实现了以下方法:

代码语言:javascript
运行
复制
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,它也弄乱了我的滚动。有时我不能再滚动了..但是如果我评论它,它工作得很好

EN

回答 3

Stack Overflow用户

发布于 2018-05-08 19:38:04

我已经创建了以下方法,并且在每种情况下都工作得很好

在XCODE中测试编辑

将其放入UITabbarController的子类中

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2019-08-14 23:14:48

在你的prepare for segue中设置hidesBottomBarWhenPushed it

代码语言:javascript
运行
复制
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.hidesBottomBarWhenPushed = true
}
票数 0
EN

Stack Overflow用户

发布于 2018-05-13 17:49:41

代码语言:javascript
运行
复制
@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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50232299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档