更改分段控制段背景无法正常工作

时间:2012-08-30 10:36:31

标签: iphone objective-c xcode

我在uitableview的标题中添加了一个分段控件。这很好用。但由于某种原因,我无法使分段按钮(或至少只是第一个按钮)具有红色背景颜色。它只是加载默认的银色。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* NEWview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    NEWview.backgroundColor = [UIColor colorWithRed:78.0/255.0f green:88.0/255.0f blue:74.0/255.0f alpha:1.0]; 
    NSArray *itemArray = [NSArray arrayWithObjects: @"Organisations", @"Events", nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmentedControl.frame = CGRectMake(15, 5, 290, 30);
    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
    segmentedControl.selectedSegmentIndex = 1;
    UIColor *newSelectedTintColor = [UIColor redColor];
    [[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];
    [NEWview addSubview:segmentedControl];
    return NEWview;
}

任何想法?提前感谢您的帮助..

2 个答案:

答案 0 :(得分:0)

请参阅this链接以更改所选片段的背景颜色

U也可以设置标题颜色,而不是这样选择:

NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [UIFont fontWithName:@"Arial" size:20.0],UITextAttributeFont,
                                  [UIColor colorWithRed:75.0/255.0 green:75.0/255.0 blue:75.0/255.0 alpha:1.0], UITextAttributeTextColor,
                                  [UIColor clearColor], UITextAttributeTextShadowColor,
                                  [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                                  nil];//[NSDictionary dictionaryWithObject:  [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentCtrl setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];

NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [UIFont fontWithName:@"Arial" size:20.0],UITextAttributeFont,
                                    [UIColor redColor], UITextAttributeTextColor,
                                    [UIColor clearColor], UITextAttributeTextShadowColor,
                                    [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                                    nil] ;//[NSDictionary dictionaryWithObject:  [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentCtrl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];

答案 1 :(得分:0)

分段控件的色调仅在分段控件为条形时才有效。

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor=[UIColor redColor];