自定义 UISegmentedControl,添加背景图像和选中段的色调颜色。

3

这是此问题的副本,但对我没有用。

我使用UICatalog创建了UISegmentedControl并尝试更改选定段的颜色。 我使用此代码来更改颜色。 背景图像很好地工作,但它不会更改所选段的颜色。 我需要做哪些修改? 或者有其他解决方案吗? 下面是我的代码。

    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];
        }
    }
}
3个回答

1
在iOS 7中,由于tintColor的新行为,尝试设置背景颜色而不是文本颜色。这将在选择时更改segmentedControl的文本颜色。
在将segmentedControl添加到视图之前,请添加此行:
segmentedControl.backgroundColor = [UIColor greenColor];

所以您不再需要这个了:

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

请记住,未选中的分段控件的背景颜色也会改变。但如果您有自定义图像,则看不到它。
希望对您有所帮助。

1
使用setBackgroundImage:forState:barMetrics:方法,状态为UIControlStateSelected

0

对于 UISegmentedControl,您可以使用以下代码

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];
        }
       else 
        {
            [[sender.subviews objectAtIndex:i] setTintColor:nil];
        }
    }

关于背景图片

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接