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. dbms_stats.gather_table_stats与analyze table 的区别[转贴]

    Analyze StatementThe ANALYZE statement can be used to gather statistics for a specific table, index ...

  2. ASP.NET MVC+Bootstrap个人博客之修复UEditor编辑时Bug(四)

    我的个人博客站在使用百度富文本编辑器UEditor修改文章时,遇到了一些问题,(不知是bug,还是我没有配置好).但总算找到了解决方法,在此记录下来. 小站首页文章列表显示为(显示去除HTML标签后的 ...

  3. 关于【bootstrap】中,【tooltip】的不算bug的bug的个人看法

    先说下遇到这个问题的背景吧. 就是在页面中有个div,这个div右上角(或者其他位置)有个 × 的图标(这个图标添加tooltip工具提示),光标移到这个图标时,触发tooltip,提示“点击移除”这 ...

  4. php 解决微信昵称emoji表情插入MySQL报错

    在PHP接受到微信用户昵称入库的时候报错 原因:utf-8 最大3个字节,而emoji占4个字节 解决办法: 1.修改mysql 数据库的字符集,改为utf8mb4,但是前提是MySQL的版本需要5. ...

  5. BootStrap入门教程 (一) :手脚架Scaffolding(全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts))

    2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...

  6. adb常用命令介绍

    adb connect 命令格式:adb connect <host>[:<port>] 作用:connect to a device via TCP/IP,Port 5555 ...

  7. Hadoop-Map/Reduce实现实现倒排索引

    先来简单介绍一下什么是文档倒排索引 倒排索引是文档检索系统中最常见的数据结构,被广泛应用在全文搜索引擎上.主要用来存储某个单词(或词组)在一个文档或者一组文档中的存储位置的映射,即提供了一种根据内容来 ...

  8. 百度,你家云管家能靠谱点不?替你脸红!Shame on you!

    此文已提交百度云问题反馈, 坐等答复. 笔者最近下载某24+G分卷压缩文件, 24+G啊, 足足要下将近7个小时.满心欢喜的下载完却尼玛发现解压出错, 有6个文件无法解压?wrong password ...

  9. The First Pig Task

                         The First Pig Program 环境: Hadoop-1.1.2 pig-0.11.1 linux系统为CentOS6.4 jdk1.6 在伪分布 ...

  10. MySQL 大表优化方案探讨

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...