UIVegmentedControl的索引UIView

时间:2015-03-19 18:56:30

标签: ios uikit uisegmentedcontrol

我想在UISegmentedControl中更改特定段(不是选定段)的背景颜色,但实际上很难为特定段获取适当的UIView。我正在寻找一种方法:

[segmentedControl viewForSegmentAtIndex:3];

1 个答案:

答案 0 :(得分:3)

结束了以下类别:)

@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