如何为selected
的{{1}}状态设置图标图像。 UISegmentedControl
真的错过了这个功能,还是我缺少技能?
有没有办法解决这个问题?
任何回答都表示赞赏。
干杯, 拉法尔
答案 0 :(得分:18)
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
-(void)segmentAction:(UISegmentedControl*)sender {
[sender setImage:[UIImage imageNamed:@"image.png"] forSegmentAtIndex:sender.selectedSegmentIndex];
}
答案 1 :(得分: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
}
}
答案 2 :(得分:3)
[segmentControl setBackgroundImage:image
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault]
您还必须为UIControlStateNormal设置图像才能使其生效。
答案 3 :(得分:0)
您可以使用单行代码设置 [uisemageButtonRef setImage:[UIImage imageNamed:@“allbtndown.png”] forSegmentAtIndex:segmentIndex];
如果您使用Xib,请找到图像
答案 4 :(得分:0)
@ Kuldeep答案的增强
您可能在segmentcontrol中有两个或更多控件
[_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];
}
}
答案 5 :(得分:0)
快捷键4
这就是我的管理方式。进行分段控制并使用图像插入分段。
请确保将一张图像设置为所选图像,因为这将是初始选择的索引。 还要设置 backgroundColor 和 tintColor 清除,以便分段控件仅显示您的图像。
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)
}
}
希望您觉得这很有用.. :)