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

时间:2013-08-21 07:54:32

标签: iphone ios objective-c uisegmentedcontrol

this的公开,但它不适合我。

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

    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 个答案:

答案 0 :(得分:1)

使用setBackgroundImage:forState:barMetrics: UIControlStateSelected作为州。

答案 1 :(得分: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];
        }
    }
}

请记住,未选定的segmentedControl的背景颜色也会发生变化。但如果您有自定义图像,则无法看到它。

希望有所帮助。

答案 2 :(得分: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];