11

如何设置selected状态的图标图像UISegmentedControl?是UISegmentedControl真的缺少这个功能还是只是我缺少技能?

4

6 回答 6

20
   [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

-(void)segmentAction:(UISegmentedControl*)sender {

    [sender setImage:[UIImage imageNamed:@"image.png"] forSegmentAtIndex:sender.selectedSegmentIndex];

}
于 2012-05-07T10:08:11.020 回答
3

在这里,我以编程方式创建了两个 UISegmentedControl 元素,您可以设置图像图标。

UISegmentedControl *switchView=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:@"Yes",@"No",nil] autorelease]];
[switchView setFrame:CGRectMake(180,802,94,27)];
switchView.selectedSegmentIndex=1;
switchView.segmentedControlStyle=UISegmentedControlStyleBar;
[switchView setImage:[UIImage imageNamed:@"switchBlueBg.png"] forSegmentAtIndex:0];   // set icon for when selected
[switchView setImage:[UIImage imageNamed:@"switchOffPlain.png"] forSegmentAtIndex:1];  //set icon for when unselected
[switchView addTarget:self action:@selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];             
[scrollview addSubview:switchView];

UISegmentedControl *switchView1=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:@"Yes",@"No",nil] autorelease]];
[switchView1 setFrame:CGRectMake(180,1003,94,27)];
switchView1.selectedSegmentIndex=1;
switchView1.segmentedControlStyle=UISegmentedControlStyleBar;
[switchView setImage:[UIImage imageNamed:@"switchBlueBg.png"] forSegmentAtIndex:0];  // set icon for when selected
[switchView setImage:[UIImage imageNamed:@"switchOffPlain.png"] forSegmentAtIndex:1];  ////set icon for when unselected
[switchView1 addTarget:self action:@selector(checkOnOffState1:) forControlEvents:UIControlEventValueChanged];
[scrollview addSubview:switchView1];



-(IBAction)checkOnOffState:(id)sender
{
    UISegmentedControl* tempSeg=(UISegmentedControl *)sender;

    if(tempSeg.selectedSegmentIndex==0)
    {
        //your codes
    }
    else
    {
        //your codes
    }   
}
于 2012-05-07T10:06:43.093 回答
3
[segmentedControl setBackgroundImage:image 
                            forState:UIControlStateSelected
                          barMetrics:UIBarMetricsDefault]

您还必须设置图像才能UIControlStateNormal使其生效。

苹果文档

于 2015-07-29T16:39:40.277 回答
1

斯威夫特 4

这就是我管理它的方式。使用图像制作分段控件并插入分段。

请务必将一张图像设置为选中,因为这将是初始选择的索引。还要将backgroundColortintColor设置为清除,以便分段控件仅显示您的图像。

var newSegmentedControl: UISegmentedControl = {
        let segmentedControl = UISegmentedControl()

        segmentedControl.insertSegment(with: UIImage(named: "x_Selected.jpg"), at: 0, animated: true)
        segmentedControl.insertSegment(with: UIImage(named: "y_Normal.jpg"), at: 1, animated: true)

        segmentedControl.addTarget(self, action: #selector(segmentedControlChanged), for: .valueChanged)

        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.backgroundColor = .clear
        segmentedControl.tintColor = .clear            

    }()

当分段控件的值发生变化时调用的函数。

@objc func segmentedControlChanged(sender: UISegmentedControl) {

    if sender.selectedSegmentIndex == 0 {
        sender.setImage(UIImage(named: "x_Selected.jpg"), forSegmentAt: sender.selectedSegmentIndex)
        sender.setImage(UIImage(named: "y_Normal.jpg"), forSegmentAt: 1)
    } else if sender.selectedSegmentIndex == 1 {
        sender.setImage(UIImage(named: "x_Normal.jpg"), forSegmentAt: 0)
        sender.setImage(UIImage(named: "y_Selected.jpg"), forSegmentAt: sender.selectedSegmentIndex)
    } 

}

希望你觉得这个有用.. :)

于 2019-03-12T00:02:57.813 回答
0

您可以使用单行代码进行设置[uisegmentButtonRef setImage:[UIImage imageNamed:@"allbtndown.png"] forSegmentAtIndex:segmentIndex];

如果您使用 Xib,请找到图片

在此处输入图像描述

于 2012-05-07T10:07:01.490 回答
0

增强@Kuldeep 的答案

段控制中可能有两个或多个控件

[_segmentcontrol addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

如果您想以不同于使用此代码的方式替换所有图像..

-(void)segmentAction:(UISegmentedControl*)sender
{
    if(sender.selectedSegmentIndex==0)
    {
        [sender setImage:[UIImage imageNamed:@"calf_selected.png"] forSegmentAtIndex:sender.selectedSegmentIndex];//0 index
        [sender setImage:[UIImage imageNamed:@"wrist.png"] forSegmentAtIndex:1];
    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"calf.png"] forSegmentAtIndex:sender.selectedSegmentIndex];//for 1 index
        [sender setImage:[UIImage imageNamed:@"wrist_selected.png"] forSegmentAtIndex:0];
    }
}
于 2014-10-15T13:55:17.277 回答