【问题标题】:How to change the selected and unselected segment's colour in UISegmentedcontrol?如何更改 UISegmentedcontrol 中选中和未选中段的颜色?
【发布时间】:2016-01-12 23:39:58
【问题描述】:

I want the UISegmentedcontrol like this

我想更改选中和未选中段的颜色。 谁能帮帮我?

【问题讨论】:

  • 这很可能是NSView的自定义子类
  • 我希望选中的段颜色为浅蓝色,未选中的段为深蓝色

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

试试这样的

self.segmantControl.backgroundColor = [UIColor colorWithRed:114.0/255. green:197./255. blue:182./255. alpha:1.0];

// 如果你在 IB/storyboard 中没有约束,你应该以编程方式设置高度约束

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.segmantControl
                                                              attribute:NSLayoutAttributeHeight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:nil
                                                              attribute:NSLayoutAttributeWidth
                                                             multiplier:1
                                                               constant:55];
[self.segmantControl addConstraint:constraint];

// 如果你在 IB/storyboard 中有约束,你应该将属性设置为高度约束,名称为 (segmentHeight)

self.segmentHeight.constant = 55; // height constraint, U should add constraints to your segment control and set property to height constraint, and change it

self.segmantControl.layer.cornerRadius = 25.0;
self.segmantControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmantControl.layer.borderWidth = 1.0f;
self.segmantControl.layer.masksToBounds = YES;

// color selected text ---> whiteColor
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateSelected];
// color disabled text ---> whiteColor
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateNormal];

UIColor *newSelectedTintColor = [UIColor colorWithRed:109./255. green:175./255. blue:179./255 alpha:1.0];
[[[self.segmantControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];//selected left segment color
[[[self.segmantControl subviews] objectAtIndex:1] setTintColor:newSelectedTintColor];// selected right segment colour

结果:

【讨论】:

  • 黄色背景不显示..加载时显示红色,但下一段的背景为空白。我也需要该背景的颜色..即未选中的。
  • @AkhilJiji,很好,复制并粘贴我的代码,不记得约束了
  • 你能告诉我如何设置吗? “self.segmentedToggle.constant = 55;” .我收到类似“在 UISegmentedControl 类型的对象上找不到属性内容”的错误
  • @AkhilJiji,嘿 self.segmentedToggle.constant - 不起作用!在我的代码中你可以看到 self.segmentHeight.constant = 55, segmentHeight - 是高度约束的属性,你现在如何使用约束?
  • 尽管我用 NsLayout 添加了你的代码,但高度仍然保持不变。
【解决方案2】:

试试这个

self.segmentedControl.tintColor = [UIColour greenColor];

单击任何段后,更改所选段的颜色:

- (void)selectedSegment:(UISegmentedControl *)sender
{
    for (int i=0; i<[sender.subviews count]; i++) 
{
        if ([[sender.subviews objectAtIndex:i]isSelected]) 
{
    UIColor *tintcolor = [UIColor blueColor];
    [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
}
 else 
{
            [[sender.subviews objectAtIndex:i] setTintColor:nil];
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-01-15
    • 2016-07-28
    • 2019-10-19
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 2012-05-12
    • 2017-05-23
    相关资源
    最近更新 更多