Change the color of selected segment control

后端 未结 4 372
离开以前
离开以前 2021-01-02 23:18

In my app,i able to change the color of the selected segment control.But the color is changed for another index rather than selected index. I can find any mistake in the ind

4条回答
  •  一整个雨季
    2021-01-02 23:45

    I'd recommend to create the two colors outside of your condition, makes your code a bit smaller. Then you can use a foreach to iterate over your segments :

    UIColor *selectedColor = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
    UIColor *deselectedColor = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
    
    for (UIControl *subview in [SegmentRound subviews]) {
        if ([subview isSelected]) 
           [subview setTintColor:selectedColor]; 
        else
           [subview setTintColor:deselectedColor]; 
    }
    

提交回复
热议问题