如何在iOS8中使用Swift轻触时隐藏/显示tabBar [英] How do I hide/show tabBar when tapped using Swift in iOS8

查看:594
本文介绍了如何在iOS8中使用Swift轻触时隐藏/显示tabBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用标签栏模仿UINavigationController的新 hidesBarsOnTap 。我已经看到很多答案,要么指向在viewController上设置 hidesBottomBarWhenPushed ,它只能完全隐藏它而不是在点击时。

I am trying to mimic the UINavigationController's new hidesBarsOnTap with a tab bar. I have seen many answers to this that either point to setting the hidesBottomBarWhenPushed on a viewController which only hides it entirely and not when tapped.

 @IBAction func tapped(sender: AnyObject) {

    // what goes here to show/hide the tabBar ???


}

提前付款

编辑:根据下面的建议我试过

as per the suggestion below I tried

self.tabBarController?.tabBar.hidden = true

确实隐藏了tabBar(在点按时切换true / false),但没有动画。我会问这是一个单独的问题。

which does indeed hide the tabBar (toggles true/false on tap), but without animation. I will ask that as a separate question though.

推荐答案

经过多次狩猎并尝试各种方法来优雅地隐藏/显示UITabBar使用Swift我能够通过 danh这个伟大的解决方案并将其转换为Swift:

After much hunting and trying out various methods to gracefully hide/show the UITabBar using Swift I was able to take this great solution by danh and convert it to Swift:

func setTabBarVisible(visible:Bool, animated:Bool) {

//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time

    // bail if the current state matches the desired state
    if (tabBarIsVisible() == visible) { return }

    // get a frame calculation ready
    let frame = self.tabBarController?.tabBar.frame
    let height = frame?.size.height
    let offsetY = (visible ? -height! : height)

    // zero duration means no animation
    let duration:NSTimeInterval = (animated ? 0.3 : 0.0)

    //  animate the tabBar
    if frame != nil {
        UIView.animateWithDuration(duration) {
            self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!)
            return
        }
    }
}

func tabBarIsVisible() ->Bool {
    return self.tabBarController?.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame)
}

// Call the function from tap gesture recognizer added to your view (or button)

@IBAction func tapped(sender: AnyObject) {
    setTabBarVisible(!tabBarIsVisible(), animated: true)
}

这篇关于如何在iOS8中使用Swift轻触时隐藏/显示tabBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆