- Support Swift 5.
- Super easy to implement page view controller with indicator bar.
- Simplistic, yet highly extensive customisation.
- Full support for custom components.
- Built on a powerful and informative page view controller.
- Header scrollable, if the width of buttons is larger than the width of the current view.
LZViewPager requires iOS 12+ (Tested on iOS 17), Swift 5.
LZViewPager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'LZViewPager'
And run pod install
.
If your pod install doesn't work properly, maybe your pod repository needs to be updated. You can try
pod repo update
and then run
pod install
LZViewPager is also available through Carthage. Simply install carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
Since this project depends on SnapKit(A Swift Autolayout DSL for iOS & OS X). You can add LZViewPager to your Cartfile
like this:
github "SnapKit/SnapKit" ~> 5.7.0
github "ladmini/LZViewPager"
dependencies: [
.package(url: "https://github.com/ladmini/LZViewPager", .upToNextMajor(from: "1.3.3"))
]
If you prefer not to use either of the aforementioned dependency managers, you can integrate LZViewPager into your project manually. Just copy the following 3 files into your project.
- LZViewPager.swift
- LZViewPagerHeader.swift
- LZViewPagerContent.swift
-
Create a
ViewController
and provide aLZViewPagerDelegate
,LZViewPagerDataSource
-
Drag a UIView into ViewVontroller's view and set it's type to LZViewPager, then set an outlet variable as "viewPager"
-
Set the items you want to display
-
Set the hostController to self
-
viewPager.reload()
class ViewController: BaseViewController, LZViewPagerDelegate, LZViewPagerDataSource {
@IBOutlet weak var viewPager: LZViewPager!
private var subControllers:[UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
viewPager.dataSource = self
viewPager.delegate = self
viewPager.hostController = self
let vc1 = UIViewController.createFromNib(storyBoardId: "ContentViewController1")!
vc1.title = "Title1"
let vc2 = UIViewController.createFromNib(storyBoardId: "ContentViewController2")!
vc2.title = "Title2"
let vc3 = UIViewController.createFromNib(storyBoardId: "ContentViewController3")!
vc3.title = "Title3"
let vc4 = UIViewController.createFromNib(storyBoardId: "ContentViewController4")!
vc4.title = "Title4"
subControllers = [vc1, vc2, vc3, vc4]
viewPager.reload()
}
}
- Implement
LZViewPagerDataSource
.
func numberOfItems() -> Int {
return self.subControllers.count
}
func controller(at index: Int) -> UIViewController {
return subControllers[index]
}
func button(at index: Int) -> UIButton {
//Customize your button styles here
let button = UIButton()
button.setTitleColor(UIColor.black, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
return button
}
- All done! ๐
You can customize you button styles in datasource method
func button(at index: Int) -> UIButton
you can also customize other styles by implementing other datasource methods example:
func heightForHeader() -> CGFloat // default is 40
func backgroundColorForHeader() -> UIColor // default is .white
func heightForIndicator() -> CGFloat // default is 2.0
func colorForIndicator(at index: Int) -> UIColor // default is UIColor(red: 255.0/255.0, green: 36.0/255.0, blue: 79.0/255.0, alpha: 1.0)
func shouldShowIndicator() -> Bool // default is true
func widthForButton(at index: Int) -> CGFloat // default is bounds.width / count
func widthForIndicator(at index: Int) -> CGFloat // default is equals to button's width
func buttonsAligment() -> ButtonsAlignment // default is .left
func shouldEnableSwipeable() -> Bool // default is true
func leftMarginForHeader() -> CGFloat //default is 0
func rightMarginForHeader() -> CGFloat //default is 0
func shouldShowSeparator() -> Bool // default is false
func colorForSeparator()-> UIColor // default is .white
func heightForSeparator() -> CGFloat // default is 2.0
func leftMarginForSeparator() -> CGFloat //default is 0
func rightMarginForSeparator() -> CGFloat //default is 0
func topMarginForSeparator() -> CGFloat //default is 0
func cornerRadiusForIndicator() -> CGFloat //default is 0
Implementing delegate methods if needed
//required
func numberOfItems() -> Int
func controller(at index: Int) -> UIViewController
func button(at index: Int) -> UIButton
//optional
func didSelectButton(at index: Int)
func willTransition(to index: Int)
func didTransition(to index: Int)
When your datasource changed you should call reload() method to refresh. If you want to locating to some page programming you can call select(index: Int, animated: Bool) method, It is important to note that animated is an optional parameter in order to be compatible with the previous versions. If you do not specify animated, it is true by default.
func reload()
public func select(index: Int, animated: Bool = true)
public var currentIndex: Int?
- Clone this repository
- Execute carthage update
- Build and Run
If you are encountering issues with LZViewPager, please raise an issue.
- Created by Ladmini (Mail to ladmini)
- Contributed to by a growing list of others.
- Logo was designed by Tobaloidee.
Bug reports and pull requests are welcome on GitHub at https://github.com/ladmini/LZViewPager.
The library is available as open source under the terms of the MIT License.