【问题标题】:UIView at Index for UISegmentedControlUISegmentedControl 索引处的 UIView
【发布时间】:2015-05-23 01:14:43
【问题描述】:
我想更改 UISegmentedControl 内特定段(不是选定段)的背景颜色,但似乎很难为特定段获取适当的 UIView。我正在寻找类似的方法:
[segmentedControl viewForSegmentAtIndex:3];
【问题讨论】:
标签:
ios
uikit
uisegmentedcontrol
【解决方案1】:
最终使用了一个类别:)
@interface UISegmentedControl (BP)
- (UIView *)segmentAtIndex:(NSUInteger)index;
@end
@implementation UISegmentedControl (BP)
- (UIView *)segmentAtIndex:(NSUInteger)index
{
// All the views are a private subclass "UISegment", and are not ordered sanely, no problem, we can fix that
return [self.subviews sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(UIView *obj1, UIView *obj2) {
return [@(obj1.frame.origin.x) compare:@(obj2.frame.origin.x)];
}][index];
}
@end