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
(或其他适当的地方)中调用ScrollingBars
的follow
方法,并将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、补丁和其他反馈。