加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 更改所选分段控件的颜色

发布时间:2020-12-15 01:48:27 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我能够更改所选分段控件的颜色.但是为另一个索引而不是选定的索引更改颜色.我可以在索引中找到任何错误. 帮我! 我的代码如下: if([SegmentRound selectedSegmentIndex] == 0) { UIColor *newSelectedTintColor2 = [UIColor colorWithRed:
在我的应用程序中,我能够更改所选分段控件的颜色.但是为另一个索引而不是选定的索引更改颜色.我可以在索引中找到任何错误.

帮我!

我的代码如下:

if([SegmentRound selectedSegmentIndex] == 0)
    {

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor2];

        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];

        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor0];



        FLAGROUND=1;
    }

    if([SegmentRound selectedSegmentIndex] == 1)
    {
        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];

        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor0];

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor2];

        FLAGROUND=2;
    }
    if([SegmentRound selectedSegmentIndex] == 2)
    {
        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor0];

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor2];

        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];
        FLAGROUND=3;
    }

viewWillAppear中:

[SegmentRound setSelectedSegmentIndex:0];

解决方法

我建议在你的条件之外创建两种颜色,使你的代码变小.然后,您可以使用foreach迭代您的细分:

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]; 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读