2012-04-30 26 views
1

我想向UISegmentedControl添加一個小的彩色指示器(只是一個彩色的矩形)。我以爲我可以繼承UISegmentedControl並添加標誌initWithFrame這樣的:繼承UISegmentedControl以在每個片段上添加一個彩色的UIView

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     UIColor *first = [UIColor blueColor]; 
     UIColor *second = [UIColor orangeColor]; 

     UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 5, self.bounds.origin.y + 5, 10, self.bounds.size.height)]; 
     firstView.backgroundColor = first; 

     [self addSubview:firstView]; 
    } 
    return self; 
} 

當我實例化一個對象CustomSegmentedControl,我剛剛得到的藍色長方形,但沒有其他的原畫segmentedcontrol邏輯。有什麼想法嗎?謝謝。

回答

1

水晶,

是否可以使用彩色矩形圖像?如果是這樣,您可以使用

- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment 

添加圖像。就像

[segmentedControl setImage:[UIImage imageNamed:@"blueRectangle"] forSegmentAtIndex:0]; 

希望這有助於!

+0

可以爲彩色矩形設置圖像。我有同樣的問題,但我試圖擺脫使用整套.pngs。我也要繼承子類。 – piperchester