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

查看:354
本文介绍了如何自定义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. 关闭按钮为白色& 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.

提前致谢,

Rajkanth

推荐答案

你不能修改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方法代码,如下所示/ p>

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天全站免登陆