如何在iPhone中自定义UISwitch按钮?

发布于 2024-10-18 22:16:33 字数 516 浏览 6 评论 0原文

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

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

创建的按钮将...

在此处输入图像描述

默认属性是,

  1. 它包含“ON”和“ “关闭”状态
  2. 关闭按钮为白色 & 白色。 ON 按钮为蓝色

我想创建一个自定义开关,以便背景颜色和颜色开关中的文本应该更改。是否可以?请详细解释一下。

提前致谢,

拉杰坎特

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....

enter image description here

The default properties are,

  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.

Thanks in Advance,

Rajkanth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-10-25 22:16:33

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

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

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 方法代码 -

-(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];
    }   
}

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

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;

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];
    }   
}
知足的幸福 2024-10-25 22:16:33

我使用了这个解决方案:UICustomSwitch:它可以自定义 UISlider 并将最大值设置为 1。

您可以更改开关的图像、右/左文本,并在背景上使用独特的颜色(如果您不想使用图像)。

我所做的唯一更改是关于名称:UI 是为 Apple 类保留的,所以我将其更改为我自己的。

I used this solution: UICustomSwitch: it works customizing a UISlider and setting a max valure of 1.

You can change the images of your switch, the right / left text and use it with a unique color on background (if you don't want to use images).

The only change I did was about the name: UI is reserved for Apple classe, so I changed it for my own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文