Skip to content

Commit 73446c2

Browse files
Add enhancement #4: Ability to set chiclet background.
1 parent e1f4865 commit 73446c2

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

DNSCastroSegmentedControl/Base.lproj/Main.storyboard

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14C109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
33
<dependencies>
44
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
55
</dependencies>
@@ -47,23 +47,34 @@
4747
<action selector="customSegmentedControlChanged:" destination="vXZ-lx-hvc" eventType="valueChanged" id="nO3-JK-DKV"/>
4848
</connections>
4949
</view>
50+
<view contentMode="scaleToFill" placeholderIntrinsicWidth="infinite" placeholderIntrinsicHeight="40" translatesAutoresizingMaskIntoConstraints="NO" id="j9m-ls-eiU" customClass="DNSCastroSegmentedControl">
51+
<rect key="frame" x="20" y="266" width="280" height="40"/>
52+
<color key="backgroundColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
53+
<connections>
54+
<action selector="customSegmentedControlChanged:" destination="vXZ-lx-hvc" eventType="valueChanged" id="B9U-sC-5HQ"/>
55+
</connections>
56+
</view>
5057
</subviews>
5158
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
5259
<constraints>
5360
<constraint firstAttribute="trailing" secondItem="XHm-9W-22v" secondAttribute="trailing" constant="20" id="4le-Ud-K0s"/>
5461
<constraint firstAttribute="trailing" secondItem="ovI-wb-jw2" secondAttribute="trailing" constant="20" id="9Wu-6r-EUg"/>
5562
<constraint firstAttribute="centerX" secondItem="9qE-kh-mCF" secondAttribute="centerX" id="A7n-zP-9Vs"/>
63+
<constraint firstAttribute="trailing" secondItem="j9m-ls-eiU" secondAttribute="trailing" constant="20" id="Cfg-hw-IFt"/>
64+
<constraint firstItem="j9m-ls-eiU" firstAttribute="top" secondItem="9qE-kh-mCF" secondAttribute="bottom" constant="18" id="CqU-Nx-cWV"/>
5665
<constraint firstItem="ovI-wb-jw2" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="Ks2-ZK-gsx"/>
5766
<constraint firstItem="9qE-kh-mCF" firstAttribute="top" secondItem="XHm-9W-22v" secondAttribute="bottom" constant="20" id="YhT-tu-ZUw"/>
5867
<constraint firstItem="27t-dN-biq" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="20" id="aLq-dN-WFv"/>
5968
<constraint firstItem="XHm-9W-22v" firstAttribute="top" secondItem="ovI-wb-jw2" secondAttribute="bottom" constant="20" id="f3C-fx-OzS"/>
6069
<constraint firstItem="ovI-wb-jw2" firstAttribute="top" secondItem="27t-dN-biq" secondAttribute="bottom" constant="20" id="hZ5-LF-bmt"/>
6170
<constraint firstItem="XHm-9W-22v" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="izC-OW-Q5Y"/>
71+
<constraint firstItem="j9m-ls-eiU" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="x3P-RV-Jcw"/>
6272
<constraint firstAttribute="centerX" secondItem="27t-dN-biq" secondAttribute="centerX" id="yoP-dt-XcW"/>
6373
</constraints>
6474
</view>
6575
<connections>
6676
<outlet property="autosizedSegmentedControl" destination="9qE-kh-mCF" id="IDp-BT-59L"/>
77+
<outlet property="coloredSelectionBackgroundSegmentedControl" destination="j9m-ls-eiU" id="YXZ-Mj-wPR"/>
6778
<outlet property="segmentedControl" destination="ovI-wb-jw2" id="SlU-vZ-SMX"/>
6879
<outlet property="stairsSegmentedControl" destination="XHm-9W-22v" id="bHJ-hG-Enq"/>
6980
<outlet property="standardSegmentedControl" destination="27t-dN-biq" id="215-Vk-YsG"/>

DNSCastroSegmentedControl/Library/DNSCastroSegmentedControl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
///The border color of the slider. If nil, defaults to the tintColor of this view.
2828
@property (nonatomic) UIColor *selectionViewColor;
2929

30+
///The background color of the selection chiclet. If nil, defaults to [UIColor clearColor].
31+
@property (nonatomic) UIColor *selectionBackgroundColor;
32+
3033
/**
3134
* Sets the given selected index.
3235
*

DNSCastroSegmentedControl/Library/DNSCastroSegmentedControl.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
@interface DNSCastroSegmentedControl()
2020
@property (nonatomic) UIView *selectionView;
21+
@property (nonatomic) UIView *selectionBackgroundView;
2122
@property (nonatomic) NSArray *sectionViews;
2223
@property (nonatomic) CGPoint initialTouchPoint;
2324
@property (nonatomic) NSLayoutConstraint *selectionLeftConstraint;
@@ -41,6 +42,7 @@ - (void)layoutSubviews
4142
[self setupKVO];
4243
[self setupSectionViews];
4344
[self setupSelectionView];
45+
[self setupSelectionBackgroundView];
4446
[self roundAllTheThings];
4547
[self setSelectedSegmentIndex:self.selectedSegmentIndex animated:NO];
4648
}
@@ -125,11 +127,52 @@ - (void)setupSelectionView
125127
[self addConstraint:self.selectionLeftConstraint];
126128
}
127129

130+
- (void)setupSelectionBackgroundView
131+
{
132+
self.selectionBackgroundView = [[UIView alloc] init];
133+
self.selectionBackgroundView.translatesAutoresizingMaskIntoConstraints = NO;
134+
135+
if (self.selectionBackgroundColor) {
136+
self.selectionBackgroundView.backgroundColor = self.selectionBackgroundColor;
137+
} else {
138+
self.selectionBackgroundView.backgroundColor = [UIColor clearColor];
139+
}
140+
141+
//Insert below the labels
142+
[self insertSubview:self.selectionBackgroundView belowSubview:[self.sectionViews firstObject]];
143+
144+
//Pin in all directions to the chiclet.
145+
[self pinView:self.selectionBackgroundView
146+
toOtherView:self.selectionView
147+
attribute:NSLayoutAttributeRight];
148+
[self pinView:self.selectionBackgroundView
149+
toOtherView:self.selectionView
150+
attribute:NSLayoutAttributeLeft];
151+
[self pinView:self.selectionBackgroundView
152+
toOtherView:self.selectionView
153+
attribute:NSLayoutAttributeTop];
154+
[self pinView:self.selectionBackgroundView
155+
toOtherView:self.selectionView
156+
attribute:NSLayoutAttributeBottom];
157+
}
158+
128159
- (void)roundAllTheThings
129160
{
130161
CGFloat cornerRadius = (CGRectGetHeight(self.frame) / 2);
131162
self.layer.cornerRadius = cornerRadius;
132163
self.selectionView.layer.cornerRadius = cornerRadius - SelectionViewPadding;
164+
self.selectionBackgroundView.layer.cornerRadius = cornerRadius - SelectionViewPadding;
165+
}
166+
167+
- (void)pinView:(UIView *)view1 toOtherView:(UIView *)view2 attribute:(NSLayoutAttribute)attribute
168+
{
169+
[self addConstraint:[NSLayoutConstraint constraintWithItem:view1
170+
attribute:attribute
171+
relatedBy:NSLayoutRelationEqual
172+
toItem:view2
173+
attribute:attribute
174+
multiplier:1
175+
constant:0]];
133176
}
134177

135178
- (void)pinViewToWidth:(UIView *)view withPadding:(CGFloat)padding
@@ -383,6 +426,15 @@ - (void)setSelectionViewColor:(UIColor *)selectionViewColor
383426
}
384427
}
385428

429+
- (void)setSelectionBackgroundColor:(UIColor *)selectionBackgroundColor
430+
{
431+
_selectionBackgroundColor = selectionBackgroundColor;
432+
433+
if (self.selectionBackgroundView) {
434+
self.selectionBackgroundView.backgroundColor = selectionBackgroundColor;
435+
}
436+
}
437+
386438
- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex
387439
{
388440
if (_selectedSegmentIndex != selectedSegmentIndex) {
@@ -412,6 +464,7 @@ - (void)animateSelectionViewUpIfNeeded:(BOOL)shorten
412464
options:UIViewAnimationOptionCurveEaseOut
413465
animations:^{
414466
self.selectionView.transform = CGAffineTransformMakeScale(scaleXPercentage, scaleYPercentage);
467+
self.selectionBackgroundView.transform = CGAffineTransformMakeScale(scaleXPercentage, scaleYPercentage);
415468
[self layoutIfNeeded];
416469
}
417470
completion:nil];
@@ -497,6 +550,7 @@ - (void)touchesEndedOrCancelled
497550
options:UIViewAnimationOptionCurveEaseIn
498551
animations:^{
499552
self.selectionView.transform = CGAffineTransformIdentity;
553+
self.selectionBackgroundView.transform = CGAffineTransformIdentity;
500554
//Calls LayoutIfNeeded - no need to call it seperately.
501555
[self snapToCurrentSection:NO];
502556
}

DNSCastroSegmentedControl/ViewController.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ @interface ViewController ()
1515
@property (nonatomic, weak) IBOutlet DNSCastroSegmentedControl *segmentedControl;
1616
@property (nonatomic, weak) IBOutlet DNSCastroSegmentedControl *stairsSegmentedControl;
1717
@property (nonatomic, weak) IBOutlet DNSCastroSegmentedControl *autosizedSegmentedControl;
18+
@property (nonatomic, weak) IBOutlet DNSCastroSegmentedControl *coloredSelectionBackgroundSegmentedControl;
1819
@property (nonatomic) DNSCastroSegmentedControl *programmaticSegmentedControl;
1920
@end
2021

@@ -61,6 +62,15 @@ - (void)viewDidLoad
6162

6263
self.autosizedSegmentedControl.choiceColor = [UIColor greenColor];
6364
self.autosizedSegmentedControl.selectionViewColor = [UIColor greenColor];
65+
66+
self.coloredSelectionBackgroundSegmentedControl.choices = @[
67+
@"Colored",
68+
@"Background",
69+
];
70+
//Use a red background and no outline.
71+
self.coloredSelectionBackgroundSegmentedControl.selectionBackgroundColor = [UIColor redColor];
72+
self.coloredSelectionBackgroundSegmentedControl.selectionViewColor = [UIColor clearColor];
73+
self.coloredSelectionBackgroundSegmentedControl.choiceColor = [UIColor whiteColor];
6474
}
6575

6676
- (void)viewDidLayoutSubviews
@@ -74,11 +84,11 @@ - (void)viewDidLayoutSubviews
7484
self.programmaticSegmentedControl.backgroundColor = self.stairsSegmentedControl.backgroundColor;
7585
[self.view addSubview:self.programmaticSegmentedControl];
7686

77-
//Pin to bottom of the autosized SC
87+
//Pin to bottom of the colored SC
7888
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.programmaticSegmentedControl
7989
attribute:NSLayoutAttributeTop
8090
relatedBy:NSLayoutRelationEqual
81-
toItem:self.autosizedSegmentedControl
91+
toItem:self.coloredSelectionBackgroundSegmentedControl
8292
attribute:NSLayoutAttributeBottom
8393
multiplier:1
8494
constant:40]];

0 commit comments

Comments
 (0)