如何在 iphone 中自定义 UISwitch 按钮? [英] How to customize UISwitch button in iphone?

查看:18
本文介绍了如何在 iphone 中自定义 UISwitch 按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码创建了一个 UISwitch...

I created a UISwitch using this code...

UISwitch *switch = [[UISwitch alloc]initWithFrame:CGRectMake(110, 230, 60, 60)];
[window addSubview:switchView];
[switchView release];

创建的按钮将是....

The created button will be....

默认属性是,

  1. 它包含ON"&关闭"状态
  2. OFF 按钮是白色 &ON 按钮为蓝色颜色
  1. It contains "ON" & "OFF" states
  2. The OFF button is white & the ON button is in blue color

我想创建一个自定义的开关,使背景颜色&应该更改开关中的文本.是否可以?请详细说明.

I want to create a customized switch, so that the background color & text in the switch should be changed. Is it possible? Please explain in detail.

提前致谢,

拉杰坎特

推荐答案

除非您编写自己的控件,否则您不能修改 UISwitch 控件,

You can not modify UISwitch control unless and until you write your own control,

但目前最好的方法是,您可以使用 UISegmentControl 并处理其上的事件来切换 on.png 和 off.png 图像.

But best way so far, you can used UISegmentControl and handle event on it to switch the on.png and off.png images.

UISegmentedControl* switchView=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:@"On",@"Off",nil] autorelease]];
    [switchView setFrame:CGRectMake(20,365,140,28)];
    switchView.selectedSegmentIndex=0;
    switchView.segmentedControlStyle=UISegmentedControlStyleBar;
    [switchView setImage:[UIImage imageNamed:@"onSelected.png"] forSegmentAtIndex:0];
    [switchView setImage:[UIImage imageNamed:@"off.png"] forSegmentAtIndex:1];
    [switchView addTarget:self action:@selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];

    self.navigationItem.titleView=switchView;

并像这样编写 checkOnOffState 方法代码-

and write checkOnOffState method code like this-

-(IBAction)checkOnOffState:(id)sender{

    UISegmentedControl* tempSeg=(UISegmentedControl *)sender;
    if(tempSeg.selectedSegmentIndex==0){
        [tempSeg setImage:[UIImage imageNamed:@"onSelected.png"] forSegmentAtIndex:0];
        [tempSeg setImage:[UIImage imageNamed:@"off.png"] forSegmentAtIndex:1];
    }
    else{
        [tempSeg setImage:[UIImage imageNamed:@"on.png"] forSegmentAtIndex:0];
        [tempSeg setImage:[UIImage imageNamed:@"offSelected.png"] forSegmentAtIndex:1];
    }   
}

这篇关于如何在 iphone 中自定义 UISwitch 按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆