ScrollingBars 0.1.1

ScrollingBars 0.1.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2015年1月
SPM支持SPM

Taisuke Hori维护。



  • 作者:
  • Taisuke Hori

介绍

Analytics

这是什么?

ScrollingBars让顶部和底部条随着UIScrollView或类似视图(如UITableView或UIWebView)的滚动而滚动,就像iOS8中的Safari应用。

ScrollingBars不能与UINavigationController的导航栏一起使用,因为导航栏不提供改变其高度或位置的方法。建议创建一个通用的UIView作为顶部栏。

那么我可以用它做什么?

安装

将以下内容添加到您的Podfile中,并运行$ pod install

pod 'ScrollingBars'

如果您尚未安装或集成CocoaPods到您的项目中,您可以在这里了解更多信息。

示例

ScrollingBarsExample目录中的ScrollingBarsExample.xcworkspace是一个使用ScrollingBars的实现示例。

使用

导入ScrollingBars模块。

import ScrollingBars

使用ScrollingBarsDelegate定义您的视图控制器类。

class YourViewController: UIViewController, ScrollingBarsDelegate {

创建出口连接。

    @IBOutlet weak var topBar: UIView!
    @IBOutlet weak var bottomBar: UIToolbar!

    @IBOutlet weak var topBarTopSpaceConstraint: NSLayoutConstraint!
    @IBOutlet weak var bottomBarBottomSpaceConstraint: NSLayoutConstraint!

定义ScrollingBarsDelegate方法。

    // MARK: - ScrollingBarsDelegate

    var topBarHeight: CGFloat {
        return self.topBar.frame.size.height
    }

    var topBarMinHeight: CGFloat {
        if UIApplication.sharedApplication().statusBarFrame.size.height > 20  {
            // In-Call statusbar
            return 0
        } else {
            return UIApplication.sharedApplication().statusBarFrame.size.height
        }
    }

    var topBarPosition: CGFloat {
        get {
            return -self.topBarTopSpaceConstraint.constant
        }
        set {
            self.topBarTopSpaceConstraint.constant = -newValue
            // layout for animation
            self.topBar.layoutIfNeeded()
        }
    }

    var bottomBarHeight: CGFloat {
        return self.bottomBar.frame.size.height
    }

    var bottomBarPosition: CGFloat {
        get {
            return -self.bottomBarBottomSpaceConstraint.constant
        }
        set {
            self.bottomBarBottomSpaceConstraint.constant = -newValue
            // layout for animation
            self.bottomBar.layoutIfNeeded()
        }
    }

    var bottomBarMinHeight: CGFloat {
        return 0
    }

有关这些代理方法的返回值,请参见下面的图片。

viewDidLoad(或其他适当的地方)中调用ScrollingBarsfollow方法,并将ScrollingBar实例设置到UIScrollView代理。

    scrollingBars.follow(self.scrollView, delegate: self)
    self.scrollView.delegate = scrollingBars

    // In addition, you may need to set this.
    self.automaticallyAdjustsScrollViewInsets = false

如果您无法覆盖UIScrollView代理,可以手动传递代理UIScrollView的方法,如下所示。

   func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        scrollingBars.scrollViewWillBeginDragging(scrollView)
   }

    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        scrollingBars.scrollViewDidEndDragging(scrollView, willDecelerate: decelerate)
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        scrollingBars.scrollViewDidScroll(scrollView)
    }

    func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
        scrollingBars.scrollViewWillBeginDecelerating(scrollView)
    }

    func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool {
        return scrollingBars.scrollViewShouldScrollToTop(scrollView)
    }

如果您的视图将旋转,您可能需要更改顶部栏的高度。

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        coordinator.animateAlongsideTransition({ (context : UIViewControllerTransitionCoordinatorContext!) -> Void in
            self.updateTopBarHeight()
            }, completion: nil)
    }

    func updateTopBarHeight() {
        let orientation = UIApplication.sharedApplication().statusBarOrientation
        var height: CGFloat
        if orientation.isPortrait || UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            height = 64
        } else {
            height = 44
        }

        let isHeightChanged = self.topBarHeightConstraint.constant != height
        if isHeightChanged {
            self.topBarHeightConstraint.constant = height
            self.topBar.layoutIfNeeded()
            self.scrollingBars.refresh(animated: false)
        }
    }

要求

需要iOS 7.0和Swift。

贡献

欢迎fork、补丁和其他反馈。