Skip to content
This repository was archived by the owner on Nov 29, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Examples/Sample/TableView Example/TableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Source/DZNSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 32 additions & 1 deletion Source/DZNSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand Down