Skip to content

Commit

Permalink
Merge pull request #12 from teambition/feature/support-more-indicator…
Browse files Browse the repository at this point in the history
…-style

feature: support more customization for selection indicator
  • Loading branch information
suricforever authored May 7, 2020
2 parents 392ecb4 + 73c57c7 commit e9694a2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
19 changes: 18 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
SegmentedControl.xcworkspace/xcuserdata
## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
xcuserdata/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.

Carthage/Checkouts
Carthage/Build
18 changes: 12 additions & 6 deletions SegmentedControl/SegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ open class SegmentedControl: UIControl {
open var selectionIndicatorStyle: SegmentedControlSelectionIndicatorStyle = .none
open var selectionIndicatorColor = UIColor.black
open var selectionIndicatorHeight = SelectionIndicator.defaultHeight
/// Only available in fixed layout policy
open var selectionIndicatorEdgeInsets = UIEdgeInsets.zero
open var selectionIndicatorCornerRadius: CGFloat = 0
open var titleAttachedIconPositionOffset: (x: CGFloat, y: CGFloat) = (0, 0)

/// Use center alignment when not fill the width
open var horizontallyCenterIfPossible: Bool = true

open fileprivate(set) var titles = [NSAttributedString]() {
didSet {
Expand Down Expand Up @@ -282,7 +285,7 @@ public extension SegmentedControl {
scrollView.contentInset = UIEdgeInsets.zero
case .dynamic:
scrollView.contentSize = CGSize(width: totalSegmentsWidth() + contentInset.left + contentInset.right, height: frame.height)
if (totalSegmentsWidth() + contentInset.left + contentInset.right) < frame.width {
if horizontallyCenterIfPossible && (totalSegmentsWidth() + contentInset.left + contentInset.right) < frame.width {
let padding = (frame.width - totalSegmentsWidth()) / 2
scrollView.contentInset = UIEdgeInsets(top: 0, left: padding - contentInset.left, bottom: 0, right: padding - contentInset.right)
} else {
Expand Down Expand Up @@ -571,6 +574,7 @@ public extension SegmentedControl {

fileprivate func drawSelectionIndicator() {
selectionIndicatorLayer.frame = frameForSelectionIndicator()
selectionIndicatorLayer.cornerRadius = selectionIndicatorCornerRadius
if selectionBoxLayer.superlayer == nil {
if let _ = selectionIndicatorLayer.superlayer {
scrollView.layer.insertSublayer(selectionIndicatorLayer, above: selectionBoxLayer)
Expand Down Expand Up @@ -694,10 +698,12 @@ public extension SegmentedControl {
width: fullRect.width - (selectionIndicatorEdgeInsets.left + selectionIndicatorEdgeInsets.right),
height: fullRect.height - (selectionIndicatorEdgeInsets.top + selectionIndicatorEdgeInsets.bottom))
case .dynamic:
return CGRect(x: xPosition,
y: yPosition,
width: singleSegmentWidth(at: selectedIndex),
height: selectionIndicatorHeight)
return CGRect(
x: xPosition,
y: yPosition,
width: singleSegmentWidth(at: selectedIndex),
height: selectionIndicatorHeight
).inset(by: selectionIndicatorEdgeInsets)
}
}()
return indicatorRect
Expand Down
26 changes: 18 additions & 8 deletions SegmentedControlExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@
objects = {

/* Begin PBXBuildFile section */
D341EB501D1A838400677EAF /* SegmentedControl.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D341EB4F1D1A838400677EAF /* SegmentedControl.framework */; };
D341EB511D1A838400677EAF /* SegmentedControl.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D341EB4F1D1A838400677EAF /* SegmentedControl.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
4AA16E482463B4D400547F70 /* SegmentedControl.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4AA16E472463B4D400547F70 /* SegmentedControl.framework */; };
4AA16E492463B4D500547F70 /* SegmentedControl.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 4AA16E472463B4D400547F70 /* SegmentedControl.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D3E3B0E31C328316003EAA6E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E3B0E21C328316003EAA6E /* AppDelegate.swift */; };
D3E3B0E51C328316003EAA6E /* ExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E3B0E41C328316003EAA6E /* ExampleViewController.swift */; };
D3E3B0E81C328316003EAA6E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D3E3B0E61C328316003EAA6E /* Main.storyboard */; };
D3E3B0EA1C328316003EAA6E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3E3B0E91C328316003EAA6E /* Assets.xcassets */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
D341EB521D1A838400677EAF /* Embed Frameworks */ = {
4AA16E4A2463B4D500547F70 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
D341EB511D1A838400677EAF /* SegmentedControl.framework in Embed Frameworks */,
4AA16E492463B4D500547F70 /* SegmentedControl.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
D341EB4F1D1A838400677EAF /* SegmentedControl.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = SegmentedControl.framework; path = "/Users/hongxin/Library/Developer/Xcode/DerivedData/SegmentedControl-fegllupftdbokageotnuxdmypufc/Build/Products/Debug-iphonesimulator/SegmentedControl.framework"; sourceTree = "<absolute>"; };
4AA16E432463B4C100547F70 /* SegmentedControl.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SegmentedControl.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4AA16E472463B4D400547F70 /* SegmentedControl.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SegmentedControl.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D3E3B0DF1C328316003EAA6E /* SegmentedControlExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SegmentedControlExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
D3E3B0E21C328316003EAA6E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D3E3B0E41C328316003EAA6E /* ExampleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleViewController.swift; sourceTree = "<group>"; };
Expand All @@ -44,19 +45,28 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D341EB501D1A838400677EAF /* SegmentedControl.framework in Frameworks */,
4AA16E482463B4D400547F70 /* SegmentedControl.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
4AA16E422463B4C100547F70 /* Frameworks */ = {
isa = PBXGroup;
children = (
4AA16E472463B4D400547F70 /* SegmentedControl.framework */,
4AA16E432463B4C100547F70 /* SegmentedControl.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
D3E3B0D61C328316003EAA6E = {
isa = PBXGroup;
children = (
D341EB4F1D1A838400677EAF /* SegmentedControl.framework */,
D3E3B0E11C328316003EAA6E /* SegmentedControlExample */,
D3E3B0E01C328316003EAA6E /* Products */,
4AA16E422463B4C100547F70 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -90,7 +100,7 @@
D3E3B0DB1C328316003EAA6E /* Sources */,
D3E3B0DC1C328316003EAA6E /* Frameworks */,
D3E3B0DD1C328316003EAA6E /* Resources */,
D341EB521D1A838400677EAF /* Embed Frameworks */,
4AA16E4A2463B4D500547F70 /* Embed Frameworks */,
);
buildRules = (
);
Expand Down

0 comments on commit e9694a2

Please sign in to comment.