UISegmentedControl片段的自定义图像

时间:2012-05-22 13:03:37

标签: ios uisegmentedcontrol

有没有办法在UISegmentedcontrol中为选定的段设置自定义图像并更改f​​ontsize?

2 个答案:

答案 0 :(得分: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 :(得分:0)

快速5.x 用于更改图像:

  func addSegmented2(){

        let segmentedControl = UISegmentedControl(items: ["aa", "bb"])
        segmentedControl.frame = CGRect(x: 0, y: 240, width: 250, height: 30)
        self.view.addSubview(segmentedControl)
        let image = UIImage(named: "en")?.withRenderingMode(.**alwaysOriginal**)
        segmentedControl.setImage(image, forSegmentAt: 0)

    }