From 8045203cde44e1f51bb9271afdcd8309070e6ed8 Mon Sep 17 00:00:00 2001 From: Abhishek Patel Date: Tue, 1 Mar 2016 11:43:25 -0800 Subject: [PATCH] Property to align first and last segment to edges For designs that have the segment edges alight to the borders of the control --- .../TableView Example/TableViewController.m | 1 + Source/DZNSegmentedControl.h | 2 ++ Source/DZNSegmentedControl.m | 33 ++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Examples/Sample/TableView Example/TableViewController.m b/Examples/Sample/TableView Example/TableViewController.m index 2135a56..87bca5c 100644 --- a/Examples/Sample/TableView Example/TableViewController.m +++ b/Examples/Sample/TableView Example/TableViewController.m @@ -88,6 +88,7 @@ - (DZNSegmentedControl *)control _control.selectedSegmentIndex = 1; _control.bouncySelectionIndicator = NO; _control.height = 60.0f; + _control.alignSegmentToEdge = YES; // _control.height = 120.0f; // _control.width = 300.0f; diff --git a/Source/DZNSegmentedControl.h b/Source/DZNSegmentedControl.h index 7ef4654..398feeb 100644 --- a/Source/DZNSegmentedControl.h +++ b/Source/DZNSegmentedControl.h @@ -60,6 +60,8 @@ enum { @property (nonatomic) BOOL adjustsButtonTopInset; /** YES if the selected segment is user interaction disabled. NO if touching the segment button should still forward the actions to its targets without animating the selection indicator. Default is YES. */ @property (nonatomic) BOOL disableSelectedSegment; +/** YES if the segmented control should have had their left and right segment aligned to the left and right edges of the control. NO if the left and right segment should be centered. Default is NO. */ +@property (nonatomic) BOOL alignSegmentToEdge; /** Initializes and returns a segmented control with segments having the given titles or images. diff --git a/Source/DZNSegmentedControl.m b/Source/DZNSegmentedControl.m index 3d7d4ea..0ebe302 100644 --- a/Source/DZNSegmentedControl.m +++ b/Source/DZNSegmentedControl.m @@ -145,6 +145,21 @@ - (void)layoutSubviews if (idx == self.selectedSegmentIndex) { button.selected = YES; } + + if (self.alignSegmentToEdge) { + if (idx == 0) { + button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; + } + else if (idx == ([[self buttons] count] - 1)) { + button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; + } + else { + button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; + } + } + else { + button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; + } }]; [self configureAccessoryViews]; @@ -313,6 +328,8 @@ - (CGRect)selectionIndicatorRect frame.origin.y = (self.barPosition > UIBarPositionBottom) ? 0.0f : appropriateY; } + NSUInteger index = [[self buttons] indexOfObject:button]; + if (self.autoAdjustSelectionIndicatorWidth) { NSAttributedString *attributedString = [button attributedTitleForState:UIControlStateSelected]; @@ -325,7 +342,21 @@ - (CGRect)selectionIndicatorRect } frame.size = CGSizeMake(width, self.selectionIndicatorHeight); - frame.origin.x = (button.frame.size.width*(self.selectedSegmentIndex))+(button.frame.size.width-frame.size.width)/2; + + if (self.alignSegmentToEdge) { + if (index == 0) { + frame.origin.x = 0; + } + else if (index == ([[self buttons] count] - 1)) { + frame.origin.x = (button.frame.size.width*(self.selectedSegmentIndex))+(button.frame.size.width-frame.size.width); + } + else { + frame.origin.x = (button.frame.size.width*(self.selectedSegmentIndex))+(button.frame.size.width-frame.size.width)/2; + } + } + else { + frame.origin.x = (button.frame.size.width*(self.selectedSegmentIndex))+(button.frame.size.width-frame.size.width)/2; + } } else { frame.size = CGSizeMake(button.frame.size.width, self.selectionIndicatorHeight);