Syntax renaming : NSAttributedStringKey to NSAttributedString.Key
Fix error spelling : interporation to interpolation, if you have access this variable, please change the naming
A ViewPager with NavigationBar component based on UIPageViewController and UICollectionView, which is a convenience way to supply and manager each viewController.
I want to make UIPageViewController more intuitive for using it, like UITableView, and supply a navigationBar quickly and simply.
More importantly, when UIPageViewController scroll continuously, pageControl sometimes will get wrong index, this viewPager can help you solve it.
There are some standard dataSource and delegate implemented for generating each page and navigationBar, each of these classes have simple sample code showing in the Pod Example for BmoViewPager.
give page count and each page controller, just like using tableView
func bmoViewPagerDataSourceNumberOfPage(in viewPager: BmoViewPager) -> Int {
return YourPageCount
}
func bmoViewPagerDataSource(_ viewPager: BmoViewPager, viewControllerForPageAt page: Int) -> UIViewController {
return YourPageViewController
}using default style, only need to give the each page title
func bmoViewPagerDataSourceNaviagtionBarItemTitle(_ viewPager: BmoViewPager, navigationBar: BmoViewPagerNavigationBar, forPageListAt page: Int) -> String? {
return YourPageTitleString
}navigation item title can custom attributed
func bmoViewPagerDataSourceNaviagtionBarItemNormalAttributed(_ viewPager: BmoViewPager, navigationBar: BmoViewPagerNavigationBar, forPageListAt page: Int) -> [NSAttributedString.Key : Any]? {
return [
NSAttributedString.Key.foregroundColor : UIColor.lightGray,
NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14.0)
]
}func bmoViewPagerDataSourceNaviagtionBarItemHighlightedAttributed(_ viewPager: BmoViewPager, navigationBar: BmoViewPagerNavigationBar, forPageListAt page: Int) -> [NSAttributedString.Key : Any]? {
return [
NSAttributedString.Key.foregroundColor : UIColor.red,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14.0)
]
}if you don't want use default style, you can custom your own background view and highlighted background view
|
|
| InfiniteScroll | Custom NavigationBar animation |
|---|---|
|
|
native pageController have a default pageControl if you implement the
func presentationCount(for pageViewController: UIPageViewController) -> Int and
func presentationIndex(for pageViewController: UIPageViewController) -> Int
but sometimes the index have a little bug, if you feel the way too, hope it help you
| Native PageController continuously scroll | continuously scroll using bmoViewPager |
|---|---|
|
|
There is a property isInterpolationAnimated in BmoViewPager and BmoViewPagerNavigationBar, default value both true.
Most importantly, If you want to have scroll animation every time, the pageViewController need be initialed before BmoViewPagerDataSource ask it. and told BmoViewPager the reference by public func setReferencePageViewController(_ vc: UIViewController, at page: Int)
For example, I always keep the UIViewController of previous page, current page and next page in a dictionary cachedPageViewControllers
var cachedPageViewControllers = [Int: UIViewController]()
func bmoViewPagerDelegate(_ viewPager: BmoViewPager, didAppear viewController: UIViewController, page: Int) {
switch page {
case 0:
self.prepareCachedPageViewControllers(pages: [pageCount - 1, page, page + 1])
case 1..<(pageCount - 1):
self.prepareCachedPageViewControllers(pages: [page - 1, page, page + 1])
case pageCount - 1:
self.prepareCachedPageViewControllers(pages: [page - 1, page, 0])
default:
break
}
}
func bmoViewPagerDataSource(_ viewPager: BmoViewPager, viewControllerForPageAt page: Int) -> UIViewController {
if let cacheVC = cachedPageViewControllers[page] {
return cacheVC
} else {
let vc = createPageViewController(page)
viewPager.setReferencePageViewController(vc, at: page)
cachedPageViewControllers[page] = vc
return vc
}
}
private func prepareCachedPageViewControllers(pages: [Int]) {
cachedPageViewControllers.forEach({ (key, vc) in
if !pages.contains(key) {
cachedPageViewControllers[key] = nil
}
})
pages.forEach { (page) in
if cachedPageViewController[page] == nil {
let vc = createPageViewController(page)
viewPager.setReferencePageViewController(vc, at: page)
cachedPageViewControllers[page] = vc
}
}
}
private func createPageViewController(_ page: Int) -> UIViewController {
//TODO: return your own UIViewController
}Because BmoViewPager is based on UIPageViewController, and when the scroll view scroll to edge, it will ask UIPageViewControllerDataSource to get the next and the previous page.
At the same time, BmoViewPager save the view controller and page number in the view controller's view, it will let the this page view controller has wrong property.
To run the example project, clone the repo, and run pod install from the Example directory first.
- iOS 9.0+
- Swift 5.0+
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsCocoaPods 1.1.0+ is required to build BmoViewPager 4.0.0+.
To integrate BmoViewPager into your Xcode project using CocoaPods, specify it in your Podfile:
use_frameworks!
target '<Your Target Name>' do
pod 'BmoViewPager', '~> 5.2.0'
endThen, run the following command:
$ pod installIf you prefer not to use the dependency managers, you can integrate BmoViewPager into your project manually, just copy the BmoViewPager folder into your project.
LEE ZHE YU, tzef8220@gmail.com
BmoViewPager is available under the MIT license. See the LICENSE file for more info.






