Customizing the colors of a UISegmentedControl

后端 未结 12 1812
萌比男神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:15

    The best way I have found of doing something like this is setting different attributes for different UIControlStates on the segmented control.

    self.segmentedControl.tintColor = [UIColor cb_Grey1Color];
    self.segmentedControl.backgroundColor = [UIColor cb_Grey3Color];
    NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont cbGothamBookFontWithSize:13.0], NSFontAttributeName,
                                        [UIColor whiteColor], NSForegroundColorAttributeName,
                                        [UIColor cb_Grey1Color], NSBackgroundColorAttributeName, nil];
    [self.segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
    NSDictionary *unselectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont cbGothamBookFontWithSize:13.0], NSFontAttributeName,
                                          [UIColor cb_Grey2Color], NSForegroundColorAttributeName,
                                          [UIColor cb_Grey3Color], NSBackgroundColorAttributeName, nil];
    [self.segmentedControl setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal];
    

提交回复
热议问题