01 //这里创建一个圆角矩形的按钮
02     UIButton
*button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
03      
04 //   
能够定义的button类型有以下6种,
05 //   
typedef enum {
06 //       
UIButtonTypeCustom = 0,          自定义风格
07 //       
UIButtonTypeRoundedRect,         圆角矩形
08 //       
UIButtonTypeDetailDisclosure,    蓝色小箭头按钮,主要做详细说明用
09 //       
UIButtonTypeInfoLight,           亮色感叹号
10 //       
UIButtonTypeInfoDark,            暗色感叹号
11 //       
UIButtonTypeContactAdd,          十字加号按钮
12 //   
} UIButtonType;
13      
14     //给定button在view上的位置
15     button1.frame
= CGRectMake(20, 20, 280, 20);
16      
17     //button背景色
18     button1.backgroundColor
= [UIColor clearColor];
19      
20     //设置button填充图片
21     //[button1
setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
22      
23     //设置button标题
24     [button1
setTitle:@
"点击" forState:UIControlStateNormal];
25      
26     /*
forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
27     //以下是几种状态
28 //   
enum {
29 //       
UIControlStateNormal       = 0,         常规状态显现             
30 //       
UIControlStateHighlighted  = 1 << 0,    高亮状态显现   
31 //       
UIControlStateDisabled     = 1 << 1,    禁用的状态才会显现
32 //       
UIControlStateSelected     = 1 << 2,    选中状态             
33 //       
UIControlStateApplication  = 0x00FF0000, 当应用程序标志时           
34 //       
UIControlStateReserved     = 0xFF000000  为内部框架预留,可以不管他            
35 //   
};
36      
37     /*
38      *
默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
39      *
那么可以去掉这个功能
40     */
41     button1.adjustsImageWhenHighlighted
= NO;
42     /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
43     button1.adjustsImageWhenDisabled
= NO;
44     /*
下面的这个属性设置为yes的状态下,按钮按下会发光*/
45     button1.showsTouchWhenHighlighted
= YES;
46      
47     /*
给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
48      按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
49      触发了这个事件以后,执行butClick:这个方法,addTarget:self
的意思是说,这个方法在本类中
50      也可以传入其他类的指针*/
51     [button1
addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
52      
53      
54     //显示控件
55     [self.view
addSubview:button1];


UIButtonType:

typedef enum {
UIButtonTypeCustom = 0, // no button type 自定义,无风格
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片
UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁
} UIButtonType;

UIButton常用属性:

//设置对应状态的标题内容default is nil. title is assumed to be single line

- (void)setTitle:(NSString *)title forState:(UIControlState)state;

//设置对应状态的标题颜色

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

//设置对应状态的标题阴影颜色

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;

//设置对应状态的按钮的图片

- (void)setImage:(UIImage *)image forState:(UIControlState)state;

//设置对应状态的按钮背景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

UIButton的UIControlState   :

typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};

iOS开发基础控件--UIButton的更多相关文章

  1. iOS开发基础控件--UISegmentedControl

    UISegmentedControl全局外观设置 分段控件是我们常用的控件之一,今天把具体用法总结了下: 1.初始化UISegmentedControl [plain] view plaincopy ...

  2. iOS开发基础控件--UILabel

    UILabel 的常见属性和方法: //创建UIlabel对象 UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds]; / ...

  3. iOS开发基础控件--UITextField

    001 //初始化textfield并设置位置及大小 002   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20 ...

  4. IOS(一) 基础控件的介绍以及使用

    IOS的界面的制作,相对于Android来说 简洁了很多,虽然创建布局的方式都是两种(代码创建.布局文件) 但是Android中的xml布局文件在某些方面也属于代码创建,因为自己使用到得每一个属性 都 ...

  5. IOS(二)基本控件UIButton、简易动画、transform属性、UIImageView

    UIButton //1.设置UIButton 的左右移动 .center属性 获得 CGPoint 来修改x y //1.设置UIButton 的放大缩小 bounds属性 获得CGRect 然后通 ...

  6. IOS开发之控件篇UITabBarControllor第一章 - 介绍

    UITabBarControllor的基本样子 官方有个图介绍这个TabBar的结构,我们先来看看这个结构图 --------------------------------------------- ...

  7. 【ios开发】控件细究1:UITableView

    工作了将近两个月,共接手两个项目,在项目中用的最多的就是UITableView了,但是也是问题出现的最多的地方,由于一开始不熟练,导致很多问题花了很长时间才解决.所以利用这两天空闲时间,好好梳理一下这 ...

  8. iOS开发-DatePicker控件

    时间控件不管是Android还是iOS中都是必然存在的一个控件,具体的效果大同小异,显示日期,时间,iOS中有四种方式可以选择,Time, Date,Date and Time  , Count Do ...

  9. IOS开发之控件篇UINavigationController第一章 - 介绍

    UINavigationController是一个比较常见的控件,它连接个视图,例如一个视图走到另外一个视图,之间的联系都可以用这个NavigationController的方法 一般都会由两个部分组 ...

随机推荐

  1. 《DSP using MATLAB》示例Example 8.27

    %% ------------------------------------------------------------------------ %% Output Info about thi ...

  2. svn 操作命令

    1.第一次提交代码到svn svn import project_directory PATH 2.将文件checkout到本地svn checkout path(path是服务器上的目录) 例如:s ...

  3. vw+vh+rem响应式布局

    科普下: 平时很少用的css单位: 1.长度单位: rem:相对长度单位.相对于根元素(即html元素)font-size计算值的倍数; vw:相对于视口的宽度.视口被均分为100单位的vw; vh: ...

  4. 坚持c++,真正掌握c++(4)

    这几天复习了c++primer的第12章类的解说,尽管之前这一章也看了几遍.可是我终究认为书读百遍其义自现,如今我给出一些我学习的心得,欢迎大家一起探讨. 首先,类能够说是c++的灵魂,正由于c++中 ...

  5. linux下如何添加一个用户并且让用户获得root权限【转载】

    原文:http://www.cnblogs.com/johnw/p/5499442.html 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加 ...

  6. RK3288 指令查看LCD分辨率

    通过下面指令可以查看当前系统设置的分辨率. root@xxx:/ # cd sys/class/graphics/fb0 cd sys/class/graphics/fb0 root@xxx:/sys ...

  7. "Could not find the main class: org.apache.catalina.startup.Boostrap. Program will exit."

    尝试将 myeclipse中的编译版本修改(如,将1.5修改为1.6)

  8. 了解IHttpModule接口事件执行顺便 获取Session

    本文转载自:http://www.cnblogs.com/eflylab/archive/2008/05/29/1209767.html 最近公司一个项目让人SQL注入了-为了临时先解决这个问题,使攻 ...

  9. h5 禁止微信内置浏览器调整字体大小方法

    ios 通过重写样式控制 body { -webkit-text-size-adjust:100%!important; } android 通过重写事件控制 (function() { if (ty ...

  10. 20165226 MySort的实现

    MySort的实现 一.实验要求 研究sort的其他功能,要能改的动代码,模拟实现Linux下Sort -t : -k 2的功能. 二.代码 /** * Created by xiang on 201 ...