swift button一些简单设置
(1)按钮有下面四种类型:
|
1
2
3
4
5
6
7
|
//创建一个ContactAdd类型的按钮let button:UIButton = UIButton(type:.ContactAdd)//设置按钮位置和大小button.frame=CGRectMake(10, 150, 100, 30)//设置按钮文字button.setTitle("按钮", forState:UIControlState.Normal)self.view.addSubview(button); |
(2)对于Custom定制类型按钮,代码可简化为:
|
1
|
let button = UIButton(frame:CGRectMake(10, 150, 100, 30)) |
2,按钮的文字设置
|
1
2
3
|
button.setTitle("普通状态", forState:UIControlState.Normal) //普通状态下的文字button.setTitle("触摸状态", forState:UIControlState.Highlighted) //触摸状态下的文字button.setTitle("禁用状态", forState:UIControlState.Disabled) //禁用状态下的文字 |
3,按钮文字颜色的设置
|
1
2
3
|
button.setTitleColor(UIColor.blackColor(),forState: .Normal) //普通状态下文字的颜色button.setTitleColor(UIColor.greenColor(),forState: .Highlighted) //触摸状态下文字的颜色button.setTitleColor(UIColor.grayColor(),forState: .Disabled) //禁用状态下文字的颜色 |
4,按钮文字阴影颜色的设置
|
1
2
3
|
button.setTitleShadowColor(UIColor.greenColor(),forState:.Normal) //普通状态下文字阴影的颜色button.setTitleShadowColor(UIColor.yellowColor(),forState:.Highlighted) //普通状态下文字阴影的颜色button.setTitleShadowColor(UIColor.grayColor(),forState:.Disabled) //普通状态下文字阴影的颜色 |
5,按钮背景颜色设置
|
1
|
button.backgroundColor=UIColor.blackColor() |
6,按钮文字图标的设置
|
1
2
3
|
button.setImage(UIImage(named:"icon1"),forState:.Normal) //设置图标button.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗button.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗 |
7,设置按钮背景图片
|
1
|
button.setBackgroundImage(UIImage(named:"background1"),forState:.Normal) |
8,按钮触摸点击事件响应
|
1
2
3
4
5
6
7
8
9
10
11
|
//不传递触摸对象(即点击的按钮)button.addTarget(self,action:#selector(tapped),forControlEvents:.TouchUpInside)func tapped(){ print("tapped")}//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号button.addTarget(self,action:#selector(tapped(_:)),forControlEvents:.TouchUpInside)func tapped(button:UIButton){ print(button.titleForState(.Normal))} |
常用的触摸事件类型:
swift button一些简单设置的更多相关文章
- [02]a tag只为成button用时候设置href的办法
a tag为成button使用,把JavaScript动作处理时,有如下四种停止Event效果. <a href="#"> <a href="javas ...
- BCG菜单button的简单使用
一,新建一个BCGprojectCBCGPMenuButton,基于对话框. 二.添加一个button,并关联一个CButton类型的变量m_btn1.然后手动将类型改CBCGPMenuButton成 ...
- 【配置】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。
× 检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 5 ...
- EditPlus开发Python的简单设置
EditPlus是一个功能强大的编辑器,这里介绍用它来开发Python程序所需要的简单设置. 环境:win7 1.设置Python语法缩进: [1]工具-->设置用户自定义工具 [2]在分类面板 ...
- 自定义Button 的图片设置不显示问题。
如果你是自定义button 那么你设置图片就要用 button.imageView.image = [UIImage imageName:@""]; 如果你是给系统原生的butt ...
- Android课程---Android Studio简单设置
Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings-->Appearance-->Theme, ...
- Kali-linux安装之后的简单设置
1.更新软件源:修改sources.list文件:leafpad /etc/apt/sources.list然后选择添加以下适合自己较快的源(可自由选择,不一定要全部): #官方源deb http:/ ...
- 【转】Android Studio简单设置
原文网址:http://ask.android-studio.org/?/article/14 Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以 ...
- 李洪强iOS开发Swift篇—01_简单介绍
李洪强iOS开发Swift篇—01_简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objec ...
随机推荐
- android:windowSoftInputMode属性使用 软键盘
android:windowSoftInputMode="adjustResize|stateHidden" windowSoftInputMode属性设置值说明. <act ...
- LayoutInflater 类的使用
转 http://yxwang0615.iteye.com/blog/1711147 一个Activity里如果直接用findViewById(),对应的是setConentView()的那个layo ...
- led.c驱动框架
Makefile: obj-m += led.o ################################################ KERNEL = /home/linux--FS21 ...
- Python 函数简介之一
面向对象--->对象------>Class 面向过程--->过程------>def 函数式编程--->函数---->def 函数的介绍: def fun1(): ...
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
- Maven项目中提示:Eclipse “cannot be resolved to a type” error
我的解决办法是这个: 临时解决方法是:Clean项目 ********百度到的其他解决办法,统一归纳此处****************** (1)jdk不匹配(或不存在) 项目指定的jdk为“jdk ...
- ADO。net学习笔记
来源于网络 1. SqlConnection(DBConnection) 建立程序与数据库的链接 链接字符串有两种形式: //使用Windows验证 SSPI(安全支持提供程序接口) ...
- linux iptables 相关设置
首先在使用iptables之前敲入一下两条命令 > iptables -F #这句话的意思是清空所有的链 > iptables -X #这句话的意思是清空所有自定义的链 以上两条的含 ...
- Nginx rewrite 规则 与 proxy_pass 实现
Nginx rewrite 规则 与 proxy_pass 实现 -------------------------------------------------------------- ...
- Ubuntu 12.04下PHP环境的搭建(LAMP)
1.首先打开命令行,切换到root身份,获得最新的软件包 su root sudo apt-get install update 2.安装MySQL数据库 sudo apt-get install m ...