2011-06-30 50 views
4

我有一個UISegmentedControl 3段,我想改變一個段1的寬度,當用戶選擇任何段。這裏是代碼:iPhone - UISegmentedControl段寬度= 0?

// the UISegmentedControl is created 
[segmentedControl addTarget:self action:@selector(changeIt:) 
       forControlEvents:UIControlEventValueChanged]; 

//... 

// and this is the changeIt method: 


- (void) changeIt:(id)sender { 

    // discover the original width of segment 1, to restore it later 
    UISegmentedControl *seg = (UISegmentedControl*)sender; 
    CGFloat segWidth = [seg widthForSegmentAtIndex:1]; 
    // At this point segWidth = 0 ????????????????? 
    // note: seg is not nil at this point. Printing seg to console shows it is 
    // a valid UISegmentedControl... 

} 

任何線索?謝謝。

回答

6

文檔說「默認值是{0.0},它告訴UISegmentedControl自動調整段的大小。」

+0

oooooooooops,我錯過了。謝謝! – SpaceDog

0

你可能想使用UISegmentedController方法– removeSegmentAtIndex:animated:,要麼– insertSegmentWithTitle:atIndex:animated:– insertSegmentWithImage:atIndex:animated:,這取決於你是否希望有一個標題,或者片段的圖像。