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);