【问题标题】:how to change the tint color of disable segment in UISegmentedControl如何更改 UISegmentedControl 中禁用段的色调颜色
【发布时间】:2016-07-28 08:21:03
【问题描述】:

如何更改 UISegmentedControl 中禁用段的色调颜色。 我得到了对 segmentedControl.subviews 进行排序的解决方案,下面是 swift 代码,请将其转换为目标 c。

@IBAction func indexChanged(sender: UISegmentedControl) {

let sortedViews = sender.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )

for (index, view) in sortedViews.enumerate() {
    if index == sender.selectedSegmentIndex {
        view.tintColor = UIColor.blueColor()
    } else {
        view.tintColor = UIColor.lightGrayColor()
    }
}

}

let sortedViews = segmentedControlOutletVariable.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )
sortedViews[0].tintColor = UIColor.blueColor()

【问题讨论】:

    标签: objective-c swift uisegmentedcontrol


    【解决方案1】:

    检查以下代码:

    1.它将绿色分配给选定的段。(segment-2)

    2.将蓝色分配给未选中但启用的那些(segment-1 和 3)。

    3.为禁用的段分配浅灰色。(段4)

    -(IBAction)segmentedTapped:(UISegmentedControl*)sender{
    
    for(int i=0;i<[sender.subviews count];i++)
    {
        if ([[sender.subviews objectAtIndex:i]isEnabled])
        {
    
    
            if([[sender.subviews objectAtIndex:i]isSelected])
            {
                UIColor *tintcolor=[UIColor greenColor];
                [[sender.subviews objectAtIndex:i] setTintColor:tintcolor]; //sets green color for selected segment which is enabled
            }
            else
            {
                [[sender.subviews objectAtIndex:i] setTintColor:[UIColor blueColor]]; //sets blue colour for remaining segments which are enabled but not selected.
    
            }
        }
        else
        {
            [[sender.subviews objectAtIndex:i] setTintColor:[UIColor lightGrayColor]];//sets ight gray for disabled segment.
        }
    }
    }
    

    结果将是:

    如果您想了解更多信息,可以从这里获得:

    UISegmentedControl selected segment color

    【讨论】:

    • 其实我想改变禁用段的色调..请帮助
    • segmentcontrol subviews 数组索引如何排序?
    • @jasonfrank:我的代码会将浅灰色分配给禁用的段。你可以在那里设置你想要的颜色。
    • 假设我们有 3 个段,其中一个被选中,第二个没有被选中但启用,第三个是禁用,现在我想将第三个的颜色设置为灰色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    相关资源
    最近更新 更多