TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Apr 2017 |
Maintained by afishhhhh.
A customizable segmented control for iOS.
NSArray *titles = @[@"Left", @"Medium", @"Right"];
YUSegment *segment = [[YUSegment alloc] initWithTitles:titles];
[self.view addSubview:segment];
segment.frame = (CGRect){20, 60, [UIScreen mainScreen].bounds.size.width - 40, 44};
Change the position and size of the view.
In the Identify Inspector, change the class to "YUSegment". Then connect outlet.
@property (weak, nonatomic) IBOutlet YUSegment *segment;
You could modify some properties in Attributes Inspector.
More details in YUSegmentDemo.
Similar to UISegmentedControl, you just need the following code:
[segment addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventValueChanged];
The image is the same by default, no matter whether the image selected. If you want to show different image when a specific segment selected, you should call -replaceDeselectedImagesWithImages:
or -replaceDeselectedImageWithImage:atIndex:
.
You should use -setTitleTextAttributes:forState:
to set attributed string. For example:
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:20]};
[segment setTitleTextAttributes:attributes forState:YUSegmentedControlStateNormal];
You could set borderColor
, borderWidth
, and cornerRadius
in Attributes Inspector. If you don't use interface builder, the code should look like this:
segment.layer.borderWidth = someValue;
segment.layer.borderColor = someValue;
Note: You should use
Note: About cornerRadius
instead of layer.cornerRadius
. Because if you set cornerRaduis
for segmented control, the indicator will become rounded automatically.cornerRadius
. Both segmented control and its indicator have default value of cornerRadius
. The default value is equal to half the height of the segmented control. In version 0.2.0, the indicator will not become rounded automatically if you just set cornerRaduis
of the segmented control. You also need to set cornerRadius
for indicator manually.
YUSegment don't have property look like scrollEnabled
, you just need to set the width of each segment(segmentWidth
). This property will make the segmented control scroll horizontally.
borderWidth
and borderColor
for segmented control look like:sgement.borderWidth(1.0).borderColor([UIColor redColor]);
indicator(readonly)
, you could set attributes for indicator such as backgroundColor
, borderWidth
, borderColor
, etc.segment.indicator.backgroundColor = [UIColor redColor];
segment.indicator.layer.borderWidth = 1.0;
segment.indicator.layer.borderColor = [UIColor redColor].CGColor;
borderWidth
and borderColor
for indicator look like:segment.indicator.borderWidth(1.0).borderColor([UIColor redColor]);
This code is distributed under the terms and conditions of the MIT license.