Customizing the colors of a UISegmentedControl

后端 未结 12 1829
萌比男神i
萌比男神i 2020-12-08 07:26

Does anybody know of a way to customize the appearance of the string based UISegmentedControl? I am trying to set the background color of the cell and the text color differe

12条回答
  •  再見小時候
    2020-12-08 08:17

    Here is a sample code that works with iOS9, but it is a hack, and might not work in later versions:

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Title1", @"Title2"]];
    for (id segment in [segmentedControl subviews])
    {
        for (id view in [segment subviews])
        {
            NSString *desc = [view description];
            if ([desc containsString:@"UISegmentLabel"])
            {
                [segment setTintColor:([desc containsString:@"Title1"] ? [UIColor blueColor] : [UIColor greenColor])];
            }
        }
    }
    

提交回复
热议问题