0

我在 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;
}

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

4

2 回答 2

0

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

你还可以做的是设置标题颜色选择而不是这样选择:

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];
于 2012-08-30T10:44:56.880 回答
0

仅当分段控件为条形样式时,分段控件的色调颜色才有效。

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor=[UIColor redColor];
于 2012-08-30T10:58:16.553 回答