【问题标题】:How to Change Segment Separator Color?如何更改段分隔符颜色?
【发布时间】:2017-01-06 21:14:43
【问题描述】:

在我的项目中,我使用段控制器..他们在我的段控制器中有四个段..我的问题是我想要这个背景颜色和字体颜色以及状态选择的颜色和分隔符颜色[选择段时为白色]

喜欢这张图片

但我的屏幕是

我的代码是

- (void)viewDidLoad {
[self changeColor];

}
- (void)changeColor{

    [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:83.0f/255.0f green:198.0f/255.0f blue:255.0f/255.0f alpha:1.0]} forState:UIControlStateSelected];

    [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:197.0f/255.0f green:197.0f/255.0f blue:197.0f/255.0f alpha:1.0]} forState:UIControlStateNormal];

    [mailboxsegment setTintColor:[UIColor colorWithRed:202.0f/255.0f green:202.0f/255.0f blue:202.0f/255.0f alpha:1.0]];
     UIFont *font = [UIFont boldSystemFontOfSize:09.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                      forKey:NSFontAttributeName];
   [mailboxsegment setTitleTextAttributes:attributes forState:UIControlStateNormal];


}

我的代码我会尝试改变背景颜色和改变字体大小

【问题讨论】:

  • 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 [子视图 setTintColor:deselectedColor]; }
  • 好的,我会试试@Birendra
  • 好的,如果有任何问题,请告诉我
  • 是的默认选择第一个只改变白色..如果我点击第二段或第三个,第四个只改变黑色的段颜色..不是白色
  • 你把这段代码放在哪里了?

标签: ios objective-c uisegmentedcontrol


【解决方案1】:
please try this one 

- (void)segmentAction:(UISegmentedControl *)segment 
{ 
    UIColor *selectedColor = [UIColor whiteColor]; 
    UIColor *deselectedColor = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0]; 

    for (UIControl *subview in [segment subviews]) 
    { 
        if ([subview isSelected]) 
        [subview setTintColor:selectedColor]; 
        else 
        [subview setTintColor:deselectedColor]; 
    } 
}

【讨论】:

    【解决方案2】:

    您可以创建您的自定义segmented control。但是创建自定义segmented control 有点复杂,因为您必须为selecteddeselected 状态提供images,为separator 等提供1px image

    我建议您使用四个不同的buttons,并应用逻辑一次只选择一个button

    要获得separator 颜色的效果,请将这四个buttons 放在stack view 或普通UIView 中,并将该容器视图的背景颜色设置为您要为separator 设置的颜色。

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      相关资源
      最近更新 更多