简体   繁体   English

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

[英]Customize UISegmentedControl, add background image and selected segment tint color

Dublicate of this , but its not working for me. 这个的共同点,但它不适合我。

I have created UISegmentedControl using UICatalog and trying to change the selected segment color. 我使用UICatalog创建了UISegmentedControl并尝试更改选定的段颜色。 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];
        }
    }
}

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

In iOS 7 with the new behavior of the tintColor, try setting instead the color of the background. 在iOS 7中使用tintColor的新行为,请尝试设置背景的颜色。 This will change the text color of the segmentedControl when it's selected. 这将在选择时更改segmentedControl的文本颜色。
Add this line before adding the segmentedControl to the view: 在将segmentedControl添加到视图之前添加此行:

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

Keep in mind that the background color of the unselected segmentedControl will change also. 请记住,未选定的segmentedControl的背景颜色也会改变。 But if you have custom images, you will not see it. 但是如果你有自定义图像,你就不会看到它。

Hope that helps. 希望有所帮助。

For UISegmentedControl you can use this code 对于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];
        }
    }

For background image 对于背景图像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM