RGPageViewController is a custom UIPageViewController written in Swift. It is inspired by ICViewPager by Ilter Cengiz but with some modifications. It combines an Android-like ViewPager with the blur effect introduced in iOS7. It is fully customizable and can also be used as a replacement for UITabBar.
Simply copy RGPageViewController.swift
to your project.
Subclass RGPageViewController
and implement it's datasource
and delegate
methods.
class MainViewController: RGPageViewController, RGPageViewControllerDataSource, RGPageViewControllerDelegate {
var tabTitles: NSArray = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
let dateFormatter = NSDateFormatter()
tabTitles = dateFormatter.monthSymbols
self.datasource = self
self.delegate = self
}
}
func numberOfPagesForViewController(pageViewController: RGPageViewController) -> Int
Description: Asks the datasource about the number of pages.
Parameters: pageViewController
the RGPageViewController instance that's subject to.
Returns: the total number of pages.
func tabViewForPageAtIndex(pageViewController: RGPageViewController, index: Int) -> UIView
Description: Asks the datasource to give a view to display as a tab item.
Parameters: pageViewController
the RGPageViewController instance that's subject to.
index
the index of the tab whose view is asked.
Returns: a UIView instance that will be shown as tab at the given index.
func viewControllerForPageAtIndex(pageViewController: RGPageViewController, index: Int) -> UIViewController?
Description: Asks the datasource to give a ViewController to display as a page.
Parameters: pageViewController
the RGPageViewController instance that's subject to.
index
the index of the content whose view is asked.
Returns: a UIViewController instance whose view will be shown as content at the given index.
optional func didChangePageToIndex(index: Int)
Description: Delegate objects can implement this method if they want to be informed when a page
changed.
Parameters: index
the index of the current page.
optional func widthForTabbar() -> CGFloat
Description: Delegate objects can implement this method to set a custom width for the tabbar if attached to Left or Right.
Returns: the width of the tabbar.
optional func heightForTabbar() -> CGFloat
Description: Delegate objects can implement this method to set a custom height for the tabbar if atached to Top or Bottom.
Returns: the height of the tabbar.
optional func widthOrHeightForIndicator() -> CGFloat
Description: Delegate objects can implement this method to set a custom width or height for the
tab indicator.
Returns: the width or height of the tab indicator.
optional func widthForTabAtIndex(index: Int) -> CGFloat
Description: Delegate objects can implement this method if tabs use dynamic width
or to overwrite the default width for tabs.
Parameters: index
the index of the tab.
Returns: the width for the tab at the given index.
optional func heightForTabAtIndex(index: Int) -> CGFloat
Description: Delegate objects can implement this method if tabs use dynamic height
or to overwrite the default height for tabs.
Parameters: index
the index of the tab.
Returns: the height for the tab at the given index.
optional func colorForTabIndicator() -> UIColor
Description: Delegate objects can implement this method to specify the color of the tab indicator.
Returns: the color for the tab indicator.
optional func tintColorForTabBar() -> UIColor?
Description: Delegate objects can implement this method to specify a tint color for the tabbar.
Returns: thethe tint color for the tabbar.
All RGPageViewControllerDataSource
protocol methods must be implemented. All RGPageViewControllerDelegate
protocol methods are optional.
// MARK: - RGPageViewController Data Source
func numberOfPagesForViewController(pageViewController: RGPageViewController) -> Int {
// return the total number of pages
return self.tabTitles.count
}
func tabViewForPageAtIndex(pageViewController: RGPageViewController, index: Int) -> UIView {
// return a simple label for the tab view
let title: String = self.tabTitles.objectAtIndex(index) as String
let label: UILabel = UILabel()
label.text = title
label.sizeToFit()
return label
}
func viewControllerForPageAtIndex(pageViewController: RGPageViewController, index: Int) -> UIViewController? {
// Create a new view controller and pass suitable data.
let dataViewController = self.storyboard!.instantiateViewControllerWithIdentifier("DataViewController") as DataViewController
dataViewController.dataObject = self.tabTitles[index]
return dataViewController
}
If you need something similar to a UITabBar
but with the features of a UIPageViewController
, change your tabViewForPageAtIndex(pageViewController: RGPageViewController, index: Int)
and implement heightForTabbar()
and override the default position RGTabbarPosition.Top
.
// MARK: - RGTabbarPosition
override var tabbarPosition: RGTabbarPosition {
get {
return .Bottom
}
}
// MARK: - RGPageViewController Data Source
func tabViewForPageAtIndex(pageViewController: RGPageViewController, index: Int) -> UIView {
let title: String = self.tabTitles.objectAtIndex(index) as String
// create a RGTabBarItem and pass a title, an image and a color
// the color will be used for tinting image and text
let tabView: RGTabBarItem = RGTabBarItem(frame: CGRectMake(0.0, 0.0, self.view.bounds.width / 6.0, 49.0), text: title, image: UIImage(named: "Grid"), color: nil)
// if you want to adjust the color for selected state of the item, adjust the tintColor
tabView.tintColor = UIColor.redColor()
return tabView
}
// MARK: - RGPageViewController Delegate
func heightForTabbar() -> CGFloat {
// default height of UITabBar is 49px
return 49.0
}
Change the default orientation of the pageView by overriding pagerOrientation
.
Default: UIPageViewControllerNavigationOrientation.Horizontal
Options: Horizontal | Vertical
// MARK: - UIPageViewControllerNavigationOrientation
override var pagerOrientation: UIPageViewControllerNavigationOrientation {
get {
return .Vertical
}
}
Change the default position of the Tabbar by overriding tabbarPosition
.
Default: RGTabbarPosition.Top
Options: Top | Bottom | Left | Right
// MARK: - RGTabbarPosition
override var tabbarPosition: RGTabbarPosition {
get {
return .Left
}
}
Change the default style of the Tabbar by overriding tabbarStyle
.
Default: RGTabbarStyle.Blurred
Options: Blurred | Solid
// MARK: - RGTabbarStyle
override var tabbarStyle: RGTabbarStyle {
get {
return .Solid
}
}
Change the default style of the Tabs by overriding tabStyle
.
Default: RGTabStyle.None
Options: None | InactiveFaded
// MARK: - RGTabStyle
override var tabStyle: RGTabStyle {
get {
return .InactiveFaded
}
}
The MIT License (MIT)
Copyright (c) 2014 Ronny Gerasch
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.