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

3 回答 3

1

在具有 tintColor 新行为的 iOS 7 中,尝试设置背景颜色。这将在选择时更改分段控件的文本颜色。
在将 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 的背景颜色也会发生变化。但是,如果您有自定义图像,您将看不到它。

希望有帮助。

于 2014-03-05T09:52:10.187 回答
1

使用setBackgroundImage:forState:barMetrics:withUIControlStateSelected作为状态。

于 2013-08-21T08:02:42.787 回答
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];
于 2013-08-21T08:04:53.237 回答