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

added code to support element spacing #71

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
22 changes: 16 additions & 6 deletions Pod/Classes/BetterSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ import UIKit
indicatorViewBackgroundColor = value
case let .indicatorViewInset(value):
indicatorViewInset = value
case let .segmentHorizontalGap(value):
segmentHorizontalGap = value
case let .indicatorViewBorderWidth(value):
indicatorViewBorderWidth = value
case let .indicatorViewBorderColor(value):
Expand Down Expand Up @@ -186,6 +188,12 @@ import UIKit
@IBInspectable public fileprivate(set) var indicatorViewInset: CGFloat = 2.0 {
didSet { setNeedsLayout() }
}

/// The indicator view's inset. Defaults to 0
@IBInspectable public fileprivate(set) var segmentHorizontalGap: CGFloat = 0 {
didSet { setNeedsLayout() }
}

/// The indicator view's border width
@IBInspectable public fileprivate(set) var indicatorViewBorderWidth: CGFloat {
get {
Expand Down Expand Up @@ -256,7 +264,7 @@ import UIKit
fileprivate let selectedTitleLabelsView = UIView()
fileprivate let indicatorView = IndicatorView()
fileprivate var initialIndicatorViewFrame: CGRect?

fileprivate var tapGestureRecognizer: UITapGestureRecognizer!
fileprivate var panGestureRecognizer: UIPanGestureRecognizer!

Expand Down Expand Up @@ -295,7 +303,7 @@ import UIKit
.indicatorViewBackgroundColor(Color.indicatorViewBackground),
.selectedTitleColor(Color.selectedTitle)])
}

@available(*, unavailable, message: "Use init(frame:titles:index:options:) instead.")
convenience init() {
self.init(frame: CGRect.zero,
Expand Down Expand Up @@ -356,9 +364,9 @@ import UIKit
self.index = index
moveIndicatorViewToIndex(animated, shouldSendEvent: (self.index != oldIndex || alwaysAnnouncesValue))
}

// MARK: Indicator View Customization

/**
Adds the passed view as a subview to the indicator view

Expand Down Expand Up @@ -397,8 +405,9 @@ import UIKit

// MARK: Helpers
fileprivate func elementFrame(forIndex index: UInt) -> CGRect {
let elementWidth = (width - totalInsetSize) / CGFloat(titleLabelsCount)
return CGRect(x: CGFloat(index) * elementWidth + indicatorViewInset,
let elementWidth = (width - totalInsetSize - segmentHorizontalGap) / CGFloat(titleLabelsCount)
let elementGap = segmentHorizontalGap * CGFloat(index)
return CGRect(x: (CGFloat(index) * elementWidth) + indicatorViewInset + elementGap,
y: indicatorViewInset,
width: elementWidth,
height: height - totalInsetSize)
Expand Down Expand Up @@ -446,3 +455,4 @@ extension BetterSegmentedControl: UIGestureRecognizerDelegate {
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
}

1 change: 1 addition & 0 deletions Pod/Classes/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum BetterSegmentedControlOption {
case titleBorderWidth(CGFloat)
case titleBorderColor(UIColor)
case titleNumberOfLines(Int)
case segmentHorizontalGap(CGFloat)

/* Selected segment */
case indicatorViewBackgroundColor(UIColor)
Expand Down