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

Adds segment spacing properly #120

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions Example/BetterSegmentedControl/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ViewController: UIViewController {
control1.segments = LabelSegment.segments(withTitles: ["Recent", "Nearby", "All"],
normalFont: UIFont(name: "HelveticaNeue-Light", size: 13.0)!,
selectedFont: UIFont(name: "HelveticaNeue-Medium", size: 13.0)!)
control1.segmentSpacing = 50
// Control 2: Exclusively defined in IB

// Control 3: Many options
Expand Down
12 changes: 10 additions & 2 deletions Pod/Classes/BetterSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ import Foundation
cornerRadius = value
case let .bouncesOnChange(value):
bouncesOnChange = value
case let .segmentSpacing(value):
segmentSpacing = value
}
}
}
Expand Down Expand Up @@ -157,6 +159,10 @@ import Foundation
indicatorView.layer.borderColor = newValue?.cgColor
}
}
/// The horizontal spacing between segments.
@IBInspectable public var segmentSpacing: CGFloat = 0 {
didSet { setNeedsLayout() }
}

// MARK: Private properties
private let normalSegmentsView = UIView()
Expand All @@ -173,6 +179,7 @@ import Foundation
private var selectedSegments: [UIView] { return selectedSegmentsView.subviews }
private var segmentViews: [UIView] { return normalSegments + selectedSegments }
private var totalInsetSize: CGFloat { return indicatorViewInset * 2.0 }
private var totalSpacings: CGFloat { return segmentSpacing * CGFloat(normalSegmentCount - 1) }
private lazy var defaultSegments: [BetterSegmentedControlSegment] = {
return [LabelSegment(text: "First"), LabelSegment(text: "Second")]
}()
Expand Down Expand Up @@ -327,8 +334,9 @@ import Foundation

// MARK: Helpers
private func elementFrame(forIndex index: Int) -> CGRect {
let elementWidth = (width - totalInsetSize) / CGFloat(normalSegmentCount)
let x = CGFloat(isLayoutDirectionRightToLeft ? lastIndex - index : index) * elementWidth
let elementWidth = (width - totalInsetSize - totalSpacings) / CGFloat(normalSegmentCount)
let spacingOffset = CGFloat(index) * segmentSpacing
let x = CGFloat(isLayoutDirectionRightToLeft ? lastIndex - index : index) * elementWidth + spacingOffset
return CGRect(x: x + indicatorViewInset,
y: indicatorViewInset,
width: elementWidth,
Expand Down
1 change: 1 addition & 0 deletions Pod/Classes/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public enum BetterSegmentedControlOption {
case backgroundColor(UIColor)
case cornerRadius(CGFloat)
case bouncesOnChange(Bool)
case segmentSpacing(CGFloat)
}