【问题标题】:UISegmentedControl with tintColor doesn't show selection?带有 tintColor 的 UISegmentedControl 不显示选择?
【发布时间】:2013-03-25 20:20:05
【问题描述】:

一旦您在UISegmentedControl 上设置了tintColor,当它具有segmentedControlStyleUISegmentedControlStyleBar 时,它就不再显示您选择的按钮。 valueChanged 事件仍会触发,但所选按钮中没有任何颜色变化。如果不让控件保持默认的蓝色样式,有没有办法显示选定的按钮?

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch uisegmentedcontrol


    【解决方案1】:

    UISegmentcontrol中的选定段添加颜色

    这里发件人UISegmentControl

    for (int i=0; i<[sender.subviews count]; i++) 
    {
        if ([[sender.subviews objectAtIndex:i]isSelected] ) 
        {               
        UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];
        [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
        break;
        }
    }
    

    【讨论】:

    • 此代码仅在segmentchanged事件中有效,从其他方法手动调用时无效
    • 通过 UISegmentedControl * sender = (UISegmentedControl *)Sg 获取段控制;
    【解决方案2】:

    如果您将深色设置为 TintColor,您将无法获得选定的效果,因为使用浅色如 将此代码放在 UISegmentControl 的 change 方法中

    for (int i=0; i<[[sender subviews] count]; i++)
    {
        if ([[[sender subviews] objectAtIndex:i]isSelected] )
        {
            UIColor *tintcolor=[UIColor redColor]; //your requiremnent color here
            [[[sender subviews] objectAtIndex:i] setTintColor:tintcolor];
            break;
        }
    }
    

    试试这个

    【讨论】:

    • 不要设置blackColor,否则会遇到这个问题。
    • 实际上我已经选择了segmentindex值,我想将色调设置为绿色以选择索引,灰色设置为未选择的索引......但它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2011-04-09
    • 2017-05-23
    相关资源
    最近更新 更多