自定义UISegmentedControl,添加背景图像和选定的段色调颜色 [英] Customize UISegmentedControl, add background image and selected segment tint color

查看:451
本文介绍了自定义UISegmentedControl,添加背景图像和选定的段色调颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的公布,但它不适用于我。

Dublicate of this, but its not working for me.

我使用UICatalog创建了UISegmentedControl并尝试更改所选的段颜色。我使用这个来改变颜色。背景图像工作正常但不改变选定的段颜色。我应该做些什么修改?或者任何其他方法?我的代码如下。

I have created UISegmentedControl using UICatalog and trying to change the selected segment color. I have used this to change color. The background image works fine but its not changing the selected segment color. What modifications should i have to do? Or any other approach for same? My code below.

    NSArray *segmentTextContent = @[@"First",@"Second",@"Third"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];

    segmentedControl.frame = CGRectMake(20, 50, 280, 30);

    [segmentedControl addTarget:self
                         action:@selector(segmentAction:)
               forControlEvents:UIControlEventValueChanged];

    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.selectedSegmentIndex = 1;
    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    [segmentedControl setBackgroundImage:[UIImage imageNamed:@"navigationBar"]
                                forState:UIControlStateNormal
                              barMetrics:UIBarMetricsDefault];

    [segmentedControl setDividerImage:[UIImage imageNamed:@"divider"]
                  forLeftSegmentState:UIControlStateNormal
                    rightSegmentState:UIControlStateNormal
                           barMetrics:UIBarMetricsDefault];

    // we want attributed strings for this segmented control
    NSDictionary *textAttributes = @{ UITextAttributeTextColor:[UIColor whiteColor],
UITextAttributeFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] };
    [segmentedControl setTitleTextAttributes:textAttributes forState:UIControlStateNormal];

    textAttributes = @{ UITextAttributeTextColor:[UIColor whiteColor],
UITextAttributeFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] };
    [segmentedControl setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];

    [self.view addSubview:segmentedControl];

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


推荐答案

在iOS 7中使用tintColor的新行为,请尝试设置背景的颜色。这将改变segmentedControl选中时的文本颜色。

在将segmentedControl添加到视图之前添加此行:

In iOS 7 with the new behavior of the tintColor, try setting instead the color of the background. This will change the text color of the segmentedControl when it's selected.
Add this line before adding the segmentedControl to the view:

segmentedControl.backgroundColor = [UIColor greenColor];

所以你不再需要这个了:

So you don't need this anymore:

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

请记住背景未选择的segmentedControl的颜色也会改变。但如果你有自定义图像,你将看不到它。

Keep in mind that the background color of the unselected segmentedControl will change also. But if you have custom images, you will not see it.

希望有所帮助。

这篇关于自定义UISegmentedControl,添加背景图像和选定的段色调颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆