Skip to content

Commit cf50faa

Browse files
committed
Merge pull request #11 from hyperoslo/feature/bottom-line-view
Add bottom line view
2 parents ec877e6 + 3cce351 commit cf50faa

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

Pod/PagesDemo/AppDelegate.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
88
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
99

1010
let viewController1 = UIViewController()
11-
viewController1.view.backgroundColor = UIColor.redColor()
11+
viewController1.view.backgroundColor = .blackColor()
1212
viewController1.title = "Controller A"
1313

1414
let viewController2 = UIViewController()
15-
viewController2.view.backgroundColor = UIColor.blueColor()
15+
viewController2.view.backgroundColor = .blueColor()
1616
viewController2.title = "Controller B"
1717

1818
let viewController3 = UIViewController()
19-
viewController3.view.backgroundColor = UIColor.greenColor()
19+
viewController3.view.backgroundColor = .redColor()
2020
viewController3.title = "Controller C"
2121

2222
let viewController4 = UIViewController()
23-
viewController4.view.backgroundColor = UIColor.yellowColor()
23+
viewController4.view.backgroundColor = .yellowColor()
2424
viewController4.title = "Controller D"
2525

2626
let pages = PagesController([viewController1,
@@ -30,6 +30,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3030
])
3131

3232
pages.enableSwipe = false
33+
pages.showBottomLine = true
3334

3435
let navigationController = UINavigationController(rootViewController: pages)
3536

Source/PagesController.swift

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import UIKit
77

88
@objc(HYPPagesController) public class PagesController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
99

10+
struct Dimensions {
11+
static let bottomLineHeight: CGFloat = 1.0
12+
static let bottomLineSideMargin: CGFloat = 40.0
13+
static let bottomLineBottomMargin: CGFloat = 36.0
14+
}
15+
1016
public var startPage = 0
1117
public var setNavigationTitle = true
1218

@@ -16,12 +22,27 @@ import UIKit
1622
}
1723
}
1824

25+
public var showBottomLine = false {
26+
didSet {
27+
bottomLineView.hidden = !showBottomLine
28+
}
29+
}
30+
1931
lazy var pages = Array<UIViewController>()
2032

2133
public private(set) var currentIndex = 0
2234

2335
public var pagesDelegate: PagesControllerDelegate?
2436

37+
public private(set) var bottomLineView: UIView = {
38+
let view = UIView()
39+
view.setTranslatesAutoresizingMaskIntoConstraints(false)
40+
view.backgroundColor = .whiteColor()
41+
view.alpha = 0.4
42+
view.hidden = true
43+
return view
44+
}()
45+
2546
public convenience init(_ pages: [UIViewController],
2647
transitionStyle: UIPageViewControllerTransitionStyle = .Scroll,
2748
navigationOrientation: UIPageViewControllerNavigationOrientation = .Horizontal,
@@ -38,6 +59,10 @@ import UIKit
3859

3960
delegate = self
4061
dataSource = self
62+
63+
view.addSubview(bottomLineView)
64+
addConstraints()
65+
view.bringSubviewToFront(bottomLineView)
4166
goTo(startPage)
4267
}
4368
}
@@ -56,7 +81,7 @@ extension PagesController {
5681
completion: { [unowned self] finished in
5782
self.pagesDelegate?.pageViewController(self,
5883
setViewController: viewController,
59-
atPage: currentIndex)
84+
atPage: self.currentIndex)
6085
})
6186
if setNavigationTitle {
6287
title = viewController.title
@@ -151,13 +176,31 @@ extension PagesController {
151176
completion: { [unowned self] finished in
152177
self.pagesDelegate?.pageViewController(self,
153178
setViewController: viewController,
154-
atPage: currentIndex)
179+
atPage: self.currentIndex)
155180
})
156181
if setNavigationTitle {
157182
title = viewController.title
158183
}
159184
}
160185
}
186+
187+
private func addConstraints() {
188+
view.addConstraint(NSLayoutConstraint(item: bottomLineView, attribute: .Bottom,
189+
relatedBy: .Equal, toItem: view, attribute: .Bottom,
190+
multiplier: 1, constant: -Dimensions.bottomLineBottomMargin))
191+
192+
view.addConstraint(NSLayoutConstraint(item: bottomLineView, attribute: .Left,
193+
relatedBy: .Equal, toItem: view, attribute: .Left,
194+
multiplier: 1, constant: Dimensions.bottomLineSideMargin))
195+
196+
view.addConstraint(NSLayoutConstraint(item: bottomLineView, attribute: .Right,
197+
relatedBy: .Equal, toItem: view, attribute: .Right,
198+
multiplier: 1, constant: -Dimensions.bottomLineSideMargin))
199+
200+
view.addConstraint(NSLayoutConstraint(item: bottomLineView, attribute: .Height,
201+
relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,
202+
multiplier: 1, constant: Dimensions.bottomLineHeight))
203+
}
161204
}
162205

163206
extension Array {
@@ -179,4 +222,3 @@ func nextIndex(x: Int?) -> Int? {
179222
func prevIndex(x: Int?) -> Int? {
180223
return x.map { $0 - 1 }
181224
}
182-

0 commit comments

Comments
 (0)