2012-08-30 71 views
0

我添加了分段控件到可用視圖的標題。這工作正常。但由於某些原因,我無法使分段按鈕(或至少只是第一個按鈕)具有紅色背景色。它只是加載默認的銀色。更改分段的控制段背景不起作用

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

任何想法?在此先感謝您的幫助..

回答

0

色調顏色分段控制僅適用於分段控制爲條形風格的情況。

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

參考this鏈接,選擇這樣的

U可以做也設置標題顏色slected,而不是選片段的改變背景色:

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]; 
+0

這只是文字,我需要將選項卡的背景更改爲紅色 – user987723

+0

檢查編輯好的anser –