2013-11-24 93 views
2

我無法在iOS7中更改我的分段控件的邊框顏色。我發現後續建議其他地方計算器:uisegmentedcontrol的邊框顏色

[[UISegmentedControl appearance] setTitleTextAttributes:@{ 
                  UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
                  UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0], 
                  UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0], 
                  NSForegroundColorAttributeName : [UIColor redColor], 

                  } forState:UIControlStateSelected]; 

    [self.segmentedControl setTitleTextAttributes:@{ 
                UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
                UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0], 
                UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0], 
                NSForegroundColorAttributeName : [UIColor redColor], 

                } forState:UIControlStateNormal]; 

但在這兩種情況下,我的境界仍是藍色。我似乎無法通過settitleTextAttributes更改字體陰影,邊框顏色或其他任何內容。

如何調整邊框顏色?

回答

1

好吧,我用這個掙扎了太多。所以我做了一個工作。我最終創建了一個新的UILabel,並將其作爲子視圖添加到分段控制器中的每個項目中。

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
    NSArray * segments = @[@"Crop",@"Nutrient",@"Product Group"]; 

    [self.segmentedControl setBackgroundImage:[UIImage imageNamed:@"background-yellowgradient.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

    [self.segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar]; 
    for (int i=0; i<[self.segmentedControl.subviews count]; i++) 
    { 

     [[self.segmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor clearColor]]; 
     UILabel *l = [[UILabel alloc] init]; 
     l.text = [segments objectAtIndex:i]; 
     l.textColor = [UIColor whiteColor]; 
     l.textAlignment = NSTextAlignmentCenter; 
     l.adjustsFontSizeToFitWidth = NO; 
     l.font = [UIFont systemFontOfSize:12]; 


     CGRect f = [[self.segmentedControl.subviews objectAtIndex:0] frame]; 
     f.size.width = self.segmentedControl.frame.size.width/3; 
     f.size.height = self.segmentedControl.frame.size.height; 
     f.origin.x = 0; 
     f.origin.y = 0; 

     l.frame = f; 

     [[[self.segmentedControl subviews] objectAtIndex:i] addSubview:l]; 

    } 
} 
else{ 
    // if it's not IOS7, then do what i was doing before for ios6.1 and below 
} 
2
[segmentControlObj setTintColor:[UIColor redColor]];