ios - 在 iOS8 中使用 Swift 点击时如何隐藏/显示 tabBar

标签 ios swift ios8 uitabbarcontroller

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

 @IBAction func tapped(sender: AnyObject) {

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


}

提前致谢

编辑:按照我尝试过的以下建议

self.tabBarController?.tabBar.hidden = true

确实隐藏了 tabBar(点击时切换 true/false),但没有动画。不过,我会把它作为一个单独的问题来问。

最佳答案

经过大量搜索并尝试各种方法来使用 Swift 优雅地隐藏/显示 UITabBar,我能够采用 this great solution by danh并将其转换为 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: TimeInterval = (animated ? 0.3 : 0.0)

    //  animate the tabBar
    if frame != nil {
        UIView.animate(withDuration: duration) {
            self.tabBarController?.tabBar.frame = frame!.offsetBy(dx: 0, dy: offsetY!)
            return
        }
    }
}

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

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

@IBAction func tapped(_ sender: Any?) {
    setTabBarVisible(visible: !tabBarIsVisible(), animated: true)
}

关于ios - 在 iOS8 中使用 Swift 点击时如何隐藏/显示 tabBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27008737/

相关文章:

iphone - 如何在 UITableViewController 中重载 UITableView "reloadData"方法?

ios - Swift 如何获取对象的 "pass by value"

ios - 如何使容器 View 内的 TableView 不会在键盘弹出窗口上弹起?

ios - 为什么我的 UILabel 没有截断?

swift - 我的设备正忙 : Copying Symbol Files in Xcode 6. 4

iphone - 在 Swift 中使用 shouldPerformSegueWithIdentifier( ) 方法

ios - 使用GKSession进行点对点时如何获得精确的时钟同步?

ios - swift - 扩展 PieChartView

ios - UIActivityIndi​​catorView 隐藏属性默认设置为 YES

ios - Kal 日历演练