Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inset style options #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions Segmentio/Source/Segmentio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ open class Segmentio: UIView {

private func commonInit() {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets.zero
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
Expand Down Expand Up @@ -654,7 +653,31 @@ extension Segmentio: UICollectionViewDelegateFlowLayout {
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: segmentWidth(for: indexPath), height: collectionView.frame.height)
}


public func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
switch segmentioOptions.insetStyle {
case .none:
return .zero
case .centralized:
return insetForCentralizedElements()
case let .custom(insets: insets):
return insets
}
}

private func insetForCentralizedElements() -> UIEdgeInsets {
let firstItemIndexPath = IndexPath(row: 0, section: 0)
let firstItemWidth = segmentWidth(for: firstItemIndexPath)
let leftSpacing = (self.frame.width - firstItemWidth) / 2

let lastItemIndexPath = IndexPath(row: segmentioItems.count - 1, section: 0)
let lastItemWidth = segmentWidth(for: lastItemIndexPath)
let rightSpacing = (self.frame.width - lastItemWidth) / 2

return UIEdgeInsets(top: 0, left: leftSpacing, bottom: 0, right: rightSpacing)
}
}

// MARK: - UIScrollViewDelegate
Expand Down
13 changes: 12 additions & 1 deletion Segmentio/Source/SegmentioOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public enum SegmentioPosition {
case fixed(maxVisibleItems: Int)
}

// MARK: - Inset
public enum SegmentioInsetStyle {
case none
case centralized
case custom(insets: UIEdgeInsets)
}

// MARK: - Control options

public enum SegmentioStyle: String {
Expand Down Expand Up @@ -197,6 +204,7 @@ public struct SegmentioOptions {
var labelTextNumberOfLines: Int
var states: SegmentioStates
var animationDuration: CFTimeInterval
var insetStyle: SegmentioInsetStyle

public init() {
self.backgroundColor = .lightGray
Expand All @@ -212,6 +220,7 @@ public struct SegmentioOptions {
selectedState: SegmentioState(),
highlightedState: SegmentioState())
self.animationDuration = 0.1
self.insetStyle = .none
}

public init(backgroundColor: UIColor = .lightGray,
Expand All @@ -226,7 +235,8 @@ public struct SegmentioOptions {
segmentStates: SegmentioStates = SegmentioStates(defaultState: SegmentioState(),
selectedState: SegmentioState(),
highlightedState: SegmentioState()),
animationDuration: CFTimeInterval = 0.1) {
animationDuration: CFTimeInterval = 0.1,
insetStyle: SegmentioInsetStyle = .none) {
self.backgroundColor = backgroundColor
self.segmentPosition = segmentPosition
self.scrollEnabled = scrollEnabled
Expand All @@ -238,5 +248,6 @@ public struct SegmentioOptions {
self.labelTextNumberOfLines = labelTextNumberOfLines
self.states = segmentStates
self.animationDuration = animationDuration
self.insetStyle = insetStyle
}
}