2012-05-22 32 views
1

的自定義圖像有沒有什麼方法可以在UISegmentedcontrol中爲選定的段設置自定義圖像並更改字體大小?UISegmentedControl段

回答

3

使用下面的代碼在UIControlEventValueChanged事件目標

我加入的示例代碼。

UISegmentedControl *segmentControl = [[UISegmentedControl alloc] init]; 
[segmentControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged]; 
[self.view addSubview:segmentControl]; 
[segmentControl release]; 
segmentControl = nil; 


-(void)segmentedControlValueChanged:(UISegmentedControl *)selectedSegmentControl{ 

    int numSegments = [selectedSegmentControl.subviews count]; //getting the number of all segment sections 

    //removing all segment section images. 
    for(int i = 0; i < numSegments; i++) { 

     [selectedSegmentControl setImage:nil forSegmentAtIndex:i]; 

    }  

    //setting image to the selected segment section.  
    [selectedSegmentControl setImage:[UIImage imageNamed:@"multiple.png"] forSegmentAtIndex:selectedSegmentControl.selectedSegmentIndex]; 

} 

請記下有關字體大小變化細節。您是否希望僅更改選定的分段文字字體大小或所有分段選項卡?

無論如何,這是改變段字體大小的常用方法。請根據您的條件使用它。您可以在下面添加評論以獲得任何其他幫助。

UIFont *myFont = [UIFont boldSystemFontOfSize:12.0f]; 
NSDictionary *myAttributes = [NSDictionary dictionaryWithObject:myFont 
                 forKey:UITextAttributeFont]; 
[segmentedControl setTitleTextAttributes:myAttributes 
           forState:UIControlStateNormal]; 

請注意,這只是iOS5的工作+

+1

看喜歡你認可這個答案由你自己,因爲這是行不通的。當您通過這種錯誤的方式設置圖像時,會覆蓋現有文本並將其隱藏起來 – user2083364

+0

根本不工作! –