LazyPages is a highly customizable library that helps you to show a scrollable list of view controllers synchronized with an index. It is written in Swift 5
for iOS 11+
.
UIViewController
instances or with closures that provides them.UIViewController
.UIViewController
, not UIView
.An instance of LazyPages
can be created from the storyboard or just programmatically.
To create it from the storyboard:
UIViewController
to it. It will be the container of the Page Controller.PageIndexCollectionViewController
, and in the PageController to PageController
. Set their module to LazyPages
.UICollectionView
and link it through an outlet to the collectionView
property of the class. This collection view will represent the index; we will be able to customize the cells inside the Storyboard.In the code of our view controller, we have to link both view controllers together and set the proper data source. We can do that in the prepareForSegue
method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let identifier = segue.identifier else {
return
}
guard let segueEnum = Segue(rawValue: identifier) else {
return
}
switch segueEnum {
case .PageController:
guard let pageController = segue.destinationViewController as? PageController else {
break
}
self.pageController = pageController
pageController.dataSource = self
case .PageIndex:
guard let pageIndex = segue.destinationViewController as? PageIndexCollectionViewController else {
break
}
self.pageIndex = pageIndex
pageIndex.dataSource = self
}
}
If you want to implement it programmatically, you have to create and instance of UICollectionView
and assign it to the property of PageIndexCollectionViewController
as we explained in the storyboard case. As specified before we have to link them together in both cases:
pageController?.pageIndexController = self.pageIndex
pageIndex?.pageController = self.pageController
To populate the views, we assign the data source properties for both the index and the Page Controller. For the Page Controller we can implement the data source ourselves, or use the provided data source classes (PageControllerArrayDataSource
and PageControllerClosureDataSource
) that respectively require an array of UIViewController
or closures. We can easily define a visual state for selection in the index view by overriding the selected
property in the UICollectionViewCell
subclass that we provide in the PageIndexCollectionViewController
data source:
override var isSelected: Bool {
didSet {
if isSelected {
backgroundColor = UIColor.gray
indexLabel.textColor = UIColor.white
} else {
backgroundColor = UIColor.white
indexLabel.textColor = UIColor.black
}
}
}
If you want to customize the amount of view controllers you cache, customize the cachingNeighborsCount
property of PageController
according to your needs.
Add LazyPages
to your project through the Xcode or add the following line to your package dependencies:
.package("https://github.com/spring-media/LazyPages", from: "1.0.0")
LazyPages is available under the MIT license.
LazyPages was made in-house by Spring-Media
CΓ©sar Vargas Casaseca, [email protected], @toupper on Github, @VargasCasaseca on Twitter
Vittorio Monaco, @vittoriom on Github, @Vittorio_Monaco on Twitter
link |
Stars: 23 |
Last commit: 6 days ago |
PageIndexCollectionViewControllerDataSource
function func sizeForItem(at indexPath: IndexPath) -> CGSize
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics