Skip to content

Commit e03a095

Browse files
committed
Fixed vertical scrolling of tabs within horizontal container and tab bar width calculation.
1 parent f02ac82 commit e03a095

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1717

1818
### Changed
1919
- Updated copyright notices.
20+
- TabBar bottom border is now drawn rather than it's own subview.
2021

2122
### Fixed
2223
- Vertical scrolling of tabs within horizontal container.
24+
- Tab bar width calculation.
2325

2426
## 0.0.1 - 2017-09-21
2527
### Added

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ it, simply add the following line to your Podfile:
2020
pod 'StickyHeaderTabController'
2121
```
2222

23+
## Usage
24+
25+
### StickyHeaderView
26+
- width is set by `StickyHeaderTabController`
27+
- height should be set by you
28+
- must not have priority >= 700 in order for stretch on vertical bounce to work.
29+
- be careful of compression resistance of subviews, especially imageviews.
30+
31+
### StickyHeroView
32+
- width is set by `StickyHeaderTabController`
33+
- height should be set by you
34+
- must not have any priorities lower than 250.
35+
2336
## Author
2437

2538
Benjamin Chrobot, [email protected]

StickyHeaderTabController/Classes/StickyHeaderTabController.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ open class StickyHeaderTabController: UIViewController {
134134

135135
// Must be called first to sync scrollViews
136136
updateCompoundHeaderHeight()
137+
138+
// Correct layering of header and hero
139+
updateStickyViewLayering()
137140
}
138141

139142
private func setUpScrollView() {
@@ -236,7 +239,7 @@ open class StickyHeaderTabController: UIViewController {
236239

237240
private func setTabBarConstraints() {
238241
let tabBarTopConstraint = tabBar.topAnchor.constraint(equalTo: hero.bottomAnchor)
239-
tabBarTopConstraint.priority = 500.0
242+
tabBarTopConstraint.priority = 250.0
240243
tabBarTopConstraint.isActive = true
241244

242245
let tabBarPinnedConstraint =
@@ -297,11 +300,9 @@ open class StickyHeaderTabController: UIViewController {
297300
newHeaderHeight += tabBar.bounds.height
298301

299302
compoundHeaderHeight = newHeaderHeight
300-
301-
updateStickyFrames()
302303
}
303304

304-
private func updateStickyFrames() {
305+
private func updateStickyViewLayering() {
305306
let currentYOffset: CGFloat = -trueScrollOffset
306307

307308
// Pin header to top
@@ -320,8 +321,6 @@ open class StickyHeaderTabController: UIViewController {
320321
// Ensure hero is on top
321322
view.exchangeSubview(at: headerIndex, withSubviewAt: heroIndex)
322323
}
323-
324-
stickyGuideTopConstraint?.constant = currentYOffset
325324
}
326325

327326
/// Update the content inset + offset of all tabs for a change in compound header height.
@@ -333,7 +332,9 @@ open class StickyHeaderTabController: UIViewController {
333332

334333
/// Very much a WIP menthod.
335334
fileprivate func handleVerticalScroll() {
336-
updateStickyFrames()
335+
updateStickyViewLayering()
336+
337+
stickyGuideTopConstraint?.constant = -trueScrollOffset
337338

338339
let offsetY = trueScrollOffset - compoundHeaderHeight
339340
for tab in tabs {
@@ -375,7 +376,8 @@ open class StickyHeaderTabController: UIViewController {
375376
}
376377

377378
var doesNeedUpdate = false
378-
if stickyHeader.bounds.height != lastHeaderHeight {
379+
let isBouncing = trueScrollOffset < 0
380+
if stickyHeader.bounds.height != lastHeaderHeight && !isBouncing {
379381
doesNeedUpdate = true
380382

381383
heroTopOffsetConstraint.constant = stickyHeader.bounds.height

StickyHeaderTabController/Classes/Views/StickyHeaderTabBarView.swift

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,14 @@ open class StickyHeaderTabBarView: UICollectionView {
2626
public weak var tabDataSource: StickyHeaderTabBarViewDataSource?
2727

2828
public var bottomBorderColor: UIColor? {
29-
get {
30-
return bottomBorder.backgroundColor
31-
}
32-
set {
33-
bottomBorder.backgroundColor = newValue
29+
didSet {
30+
setNeedsDisplay()
3431
}
3532
}
3633

3734
public var bottomBorderWidth: CGFloat = 1.0 {
3835
didSet {
39-
setNeedsLayout()
36+
setNeedsDisplay()
4037
}
4138
}
4239

@@ -47,8 +44,6 @@ open class StickyHeaderTabBarView: UICollectionView {
4744

4845
private let flowLayout = UICollectionViewFlowLayout()
4946

50-
private let bottomBorder = UIView()
51-
5247
// MARK: - Initialization
5348

5449
public init(frame: CGRect) {
@@ -72,7 +67,6 @@ open class StickyHeaderTabBarView: UICollectionView {
7267

7368
setUpCollectionView()
7469
setUpFlowLayout()
75-
setUpBottomBorder()
7670
}
7771

7872
private func setUpCollectionView() {
@@ -97,12 +91,6 @@ open class StickyHeaderTabBarView: UICollectionView {
9791
right: cellSpacing)
9892
}
9993

100-
private func setUpBottomBorder() {
101-
addSubview(bottomBorder)
102-
bottomBorderWidth = 2.0
103-
bottomBorderColor = UIColor(white: 0.92, alpha: 1.0)
104-
}
105-
10694
// MARK: - Open Methods
10795

10896
open func registerCellForReuse() {
@@ -117,21 +105,23 @@ open class StickyHeaderTabBarView: UICollectionView {
117105
selectItem(at: newIndexPath, animated: animated, scrollPosition: .bottom)
118106
}
119107

120-
// MARK: - Private Methods
108+
// MARK: - Override
121109

122-
private func updateBottomBorderFrame() {
123-
bottomBorder.frame = CGRect(x: 0,
124-
y: bounds.height - bottomBorderWidth,
125-
width: bounds.width,
126-
height: bottomBorderWidth)
127-
}
110+
open override func draw(_ rect: CGRect) {
111+
guard let borderColor = bottomBorderColor, bottomBorderWidth > 0 else {
112+
super.draw(rect)
128113

129-
// MARK: - Override
114+
return
115+
}
130116

131-
override open func layoutSubviews() {
132-
super.layoutSubviews()
117+
let borderRect = CGRect(x: 0,
118+
y: rect.height - bottomBorderWidth,
119+
width: rect.width,
120+
height: rect.width)
121+
let borderPath = UIBezierPath(rect: borderRect)
133122

134-
updateBottomBorderFrame()
123+
borderColor.set()
124+
borderPath.fill()
135125
}
136126
}
137127

@@ -178,7 +168,8 @@ extension StickyHeaderTabBarView: UICollectionViewDelegateFlowLayout {
178168
let naturalWidth = StickyHeaderTabBarViewCell.cellSize(for: text).width
179169

180170
let numberOfTabs = CGFloat(collectionView.numberOfItems(inSection: 0))
181-
let viewSizeWidth = (collectionView.bounds.width - (2.0 * cellSpacing)) / numberOfTabs
171+
let spacingSumSize = CGFloat(numberOfTabs + 1) * cellSpacing
172+
let viewSizeWidth = (collectionView.bounds.width - spacingSumSize) / numberOfTabs
182173

183174
let width = max(naturalWidth, viewSizeWidth)
184175

0 commit comments

Comments
 (0)