Creating and Using Switches with UISwitch

You would like to give your users the ability to turn an option on or off.

effect as follow:

Solution
Use the UISwitch class.

Some steps to implment:

1. Let’s create a property of type UISwitch and call it mainSwitch

ViewController.m:

#import "ViewController.h"
@interface ViewController () @property (nonatomic, strong) UISwitch *mainSwitch; @end @implementation ViewController ...

2. the viewDidLoad method in your view controller’s implementation file,

Let’s create our switch and place it on our view controller’s view

- (void)viewDidLoad{
[super viewDidLoad];
       
  /* Create the switch */
  self.mySwitch = [[UISwitch alloc] initWithFrame:
                     CGRectMake(100, 100, 0, 0)];
  [self.mySwitch setOn:YES];
  [self.view addSubview:self.mySwitch];
   
  [self.mySwitch addTarget:self
                 action:@selector(switchIsChanged:)
            forControlEvents:UIControlEventValueChanged];}

Note that the parameter that we have to pass to this method is
of type CGRect. A CGRect denotes the boundaries of a rectangle using the (x,y) position
of the top-left corner of the rectangle and its width and height.

After we’ve created the switch, we simply add it to our view controller’s view.

// set the switch on

[self.mainSwitch setOn:YES];

you can read from the on property of the switch to find out whether the
switch is on or off at the moment.

Alternatively, you can use the isOn method of the switch.

eg:

if ([self.mainSwitch isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}

If you want to get notified when the switch gets turned on or off, you will need to add
your class as the target for the switch, using the addTarget:action:forControlEvents:
method of UISwitch

eg:

[self.mainSwitch addTarget:self
action:@selector(switchIsChanged:)
forControlEvents:UIControlEventValueChanged];

Then implement the switchIsChanged: method.

- (void) switchIsChanged:(UISwitch *)paramSender{
NSLog(@"Sender is = %@", paramSender); if ([paramSender isOn]){
NSLog(@"The switch is turned on.");
} else {
NSLog(@"The switch is turned off.");
}
}

Run Result:

Sender is = <UISwitch: 0x6e13500;
                  frame = (100 100; 79 27);
                  layer = <CALayer: 0x6e13700>>
The switch is turned off.

Customizing the UISwitch

There are two main ways of customizing a switch:

Tint Colors
Tint colors are colors that you can apply to a UI component such as a UISwitch.
The tint color will be applied on top of the current color of the component. For
instance, in a normal UISwitch, you will be able to see different colors. When you
apply the tint color on top, the normal color of the control will be mixed with the
tint color, giving a flavor of the tint color on the UI control.

Images
A switch has two images:
On Image
The image that represents the on state of the switch. The width of this image is
77 points, and its height is 22.

Off Image
The image that represents the switch in its off state. This image, like the on state
of the switch, is 77 points in width and 22 points in height.

tintColor
This is the tint color that will be applied to the off state of the switch.

Unfortunately, Apple has not taken the time to name this property offTintColor instead of tint
Color to make it more explicit.

thumbTintColor
This is the tint color that will be applied to the little knob on the switch.

onTintColor
This tint color will be applied to the switch in its on state.

// simple code snippet that will change the on-mode tint color of the switch to
// red, the off-mode tint color to brown, and the knob’s tint color to green.

- (void)viewDidLoad
{
[super viewDidLoad]; /* Create the switch */
self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
self.mainSwitch.center = self.view.center;
[self.view addSubview:self.mainSwitch]; /* Customize the switch */
/* Adjust the off-mode tint color */
self.mainSwitch.tintColor = [UIColor redColor]; /* Adjust the on-mode tint color */
self.mainSwitch.onTintColor = [UIColor brownColor]; /* Also change the knob's tint color */
self.mainSwitch.thumbTintColor = [UIColor greenColor];
}

Let’s move on to customizing the appearance of the switch using its on and off images

prepare a new set of on and off images.

As mentioned before, both the on and the off images in a switch should be 77 points wide and 22 points tall.

On@2x.png
Off@2x.png

- (void)viewDidLoad
{
[super viewDidLoad]; /* Create the switch */
self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
self.mainSwitch.center = self.view.center; /* Make sure the switch won't appear blurry(模糊) on iOS Simulator */
self.mainSwitch.frame
= [self roundedValuesInRect:self.mainSwitch.frame]; [self.view addSubview:self.mainSwitch]; /* Customize the switch */
self.mainSwitch.onImage = [UIImage imageNamed:@"On"];
self.mainSwitch.offImage = [UIImage imageNamed:@"Off"];
}

eg:

IOS 7 开发范例 - UISwitch的使用的更多相关文章

  1. iOS移动开发周报-第22期

    iOS移动开发周报-第22期 [摘要]:本期iOS移动开发周报带来如下内容:苹果股价创新高,iOS8自动调整UITableView布局,Swift学习心得等. 新闻 <苹果股价创新高 市值全球第 ...

  2. iOS应用开发详解

    <iOS应用开发详解> 基本信息 作者: 郭宏志    出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...

  3. iOS App开发的那些事儿2:如何搭建合适的框架

    <iOS App开发的那些事儿>系列文章从更宏观的角度出发,不仅仅局限于具体某个功能.界面的实现,而是结合网易云信iOS端研发负责人多年的经验,从如何优化现有代码的角度出发,深度分析如何创 ...

  4. iOS App开发那些事:如何选择合适的人、规范和框架?

    http://www.cocoachina.com/ios/20141202/10386.html 自从做Team Leader之后,身上权责发生了变化,于是让我烦恼的不再是具体某个功能,某个界面的实 ...

  5. 中文 iOS/Mac 开发博客列表

    中文 iOS/Mac 开发博客列表 博客地址 RSS地址 OneV's Den http://onevcat.com/atom.xml 一只魔法师的工坊 http://blog.ibireme.com ...

  6. Unity iOS混合开发界面切换思路

    Unity iOS混合开发界面切换思路 最近有很多博友QQ 私信 或则 留言联系我,请教iOS和Unity界面之前相互切换的问题,源代码就不私下发你们了,界面跳转功能的代码我直接贴到下面好了,顺带说i ...

  7. ios新手开发——toast提示和旋转图片加载框

    不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...

  8. iOS常用开发技巧

    iOS开发过程中,总有那么一些个小问题让人纠结,它们不会让程序崩溃,但是会让人崩溃.除此之外,还将分享一些细节现在我通过自己的总结以及从其他地方的引用,来总结一下一些常见小问题. 本篇长期更新,多积累 ...

  9. iOS widget开发

    链接: iOS Widget开发 iOS开发之构建Widget iOS开发Widget iOS开发-widget基础 ios8新特性widget开发 ios 10 开发-widget实现 Widget ...

随机推荐

  1. linux中waitpid及wait的用法

    wait(等待子进程中断或结束) 表头文件      #include<sys/types.h>      #include<sys/wait.h> 定义函数 pid_t wa ...

  2. ZOJ3865:Superbot(BFS) The 15th Zhejiang University Programming Contest

    一个有几个小坑的bfs 题目很长,但并不复杂,大概总结起来有这么点. 有t组输入 每组输入n, m, p.表示一个n*m的地图,每p秒按键会右移一次(这个等会儿再讲). 然后是地图的输入.其中'@'为 ...

  3. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  4. bzoj 2818 Gcd(欧拉函数 | 莫比乌斯反演)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2818 [题意] 问(x,y)为质数的有序点对的数目. [思路一] 定义f[i]表示i之 ...

  5. JAVA与数据库开发(JDBC-ODBC、SQL Server、MySQL)

    1)配置数据库环境和驱动 2)设计数据库结构并创建数据库 3)对数据库进行增删改查操作...

  6. Chrome多线程模型

    为什么使用多线程? Chrome的多线程模型主要解决什么问题? 如何实现该问题的解决? 1. 解决问题 Chrome有很多线程,这是为了保持UI线程(主线程)的高响应度,防止被其他费时的操作阻碍从而影 ...

  7. java 拷贝功能

    java 中的 拷贝分为浅拷贝 和 深拷贝 浅拷贝需要实现Cloneable接口,深拷贝需要实现Serializable接口. public class Square implements Clone ...

  8. android - python 自动化测试 移动互联网 - SegmentFault

    android - python 自动化测试 移动互联网 - SegmentFault splinter

  9. 第二百四十四、五天 how can I 坚持

    昨天忘了.不知咋忘的,加班加迷糊了? 昨天联调接口,又加班了,好歹基本调通了. 今天,下午,开会,有点被领导批的意思,不是批我,是批我们团队. 团队. 不懂自己. 这样做有意义嘛. 睡觉.好烦. 到底 ...

  10. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...