From 2674dc6f9cc921eaa82c9b8ccf78d524ce9a8105 Mon Sep 17 00:00:00 2001 From: Melaka Date: Tue, 13 Mar 2018 13:58:03 +0800 Subject: [PATCH 1/2] added code to support element spacing --- Pod/Classes/BetterSegmentedControl.swift | 20 +++++++++++++++----- Pod/Classes/Options.swift | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Pod/Classes/BetterSegmentedControl.swift b/Pod/Classes/BetterSegmentedControl.swift index 946e693..383f4df 100644 --- a/Pod/Classes/BetterSegmentedControl.swift +++ b/Pod/Classes/BetterSegmentedControl.swift @@ -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): @@ -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 { @@ -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! @@ -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, @@ -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 @@ -398,7 +406,8 @@ 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 elementGap = segmentHorizontalGap * CGFloat(index) + return CGRect(x: (CGFloat(index) * elementWidth) + indicatorViewInset + elementGap, y: indicatorViewInset, width: elementWidth, height: height - totalInsetSize) @@ -446,3 +455,4 @@ extension BetterSegmentedControl: UIGestureRecognizerDelegate { return super.gestureRecognizerShouldBegin(gestureRecognizer) } } + diff --git a/Pod/Classes/Options.swift b/Pod/Classes/Options.swift index 24366b1..0c4d52b 100644 --- a/Pod/Classes/Options.swift +++ b/Pod/Classes/Options.swift @@ -21,6 +21,7 @@ public enum BetterSegmentedControlOption { case titleBorderWidth(CGFloat) case titleBorderColor(UIColor) case titleNumberOfLines(Int) + case segmentHorizontalGap(CGFloat) /* Selected segment */ case indicatorViewBackgroundColor(UIColor) From 0bfa8284180184f44ace36e03fc2d581229ea444 Mon Sep 17 00:00:00 2001 From: Melaka Date: Tue, 13 Mar 2018 14:16:48 +0800 Subject: [PATCH 2/2] Update BetterSegmentedControl.swift fixes the elements being cut off from the edge --- Pod/Classes/BetterSegmentedControl.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pod/Classes/BetterSegmentedControl.swift b/Pod/Classes/BetterSegmentedControl.swift index 383f4df..a36ccd4 100644 --- a/Pod/Classes/BetterSegmentedControl.swift +++ b/Pod/Classes/BetterSegmentedControl.swift @@ -405,7 +405,7 @@ import UIKit // MARK: Helpers fileprivate func elementFrame(forIndex index: UInt) -> CGRect { - let elementWidth = (width - totalInsetSize) / CGFloat(titleLabelsCount) + let elementWidth = (width - totalInsetSize - segmentHorizontalGap) / CGFloat(titleLabelsCount) let elementGap = segmentHorizontalGap * CGFloat(index) return CGRect(x: (CGFloat(index) * elementWidth) + indicatorViewInset + elementGap, y: indicatorViewInset,