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 ...
随机推荐
- string转化为int方法
int intA = 0; 1.intA =int.Parse(str); 2.int.TryParse(str, out intA); 3.intA = Convert.ToInt32(str);以 ...
- 洛谷U4727 小L 的二叉树
U4727 小L 的二叉树 题目背景 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣. 所以,小L当时卡在了二叉树. 题目描述 在计算机科学中,二叉树 ...
- (转)Hadoop的InputFormats和OutputFormats
Data Mining Hadoop的InputFormats和OutputFormats InputFormat InputFormat类用来产生InputSplit,并把它切分成record. p ...
- manecher
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...
- Altera Soc交叉编译环境搭建
该文主要是对rocketboard教程的补充,详细教程参见https://rocketboards.org/foswiki/view/Documentation/EmbeddedLinuxBeginn ...
- winform无法查看设计器
在代码中右键无法查看设计器.将无效的引用去除,重新生成即可.
- Sping--AOP--Annotation
Aspectj 概念: 1. joinpoint:切入点, 比如@Before, @After, @Around 2. Pointcut:切入点集合, 比如 @Pointcut("execu ...
- spell checking
Spell checker Description You, as a member of a development team for a new spell checking program, a ...
- iOS校验身份证是否合法
//身份证号验证 1900+/2000+的年份日期的正则表达式经过修改,目前貌似是对的,如果哪位朋友发现错误希望能够给与提示 //返回yes位表示格式正确,否则为错误 -(BOOL)IDCardAut ...
- CSS3样式linear-gradient的使用(切角效果)
linear-gradient linear-gradient是CSS3中新增的样式,主要用于颜色的渐变效果.MDN地址 linear-gradient在不同内核下使用方式不同,详细内容可参考w3cp ...