iOS UIButton 属性
来源:互联网 发布:中央财经大学命案知乎 编辑:程序博客网 时间:2024/05/21 12:49
/创建UIButton对象
UIButton *bt=[[UIButton alloc]init];
//隐藏BIButton
bt.hidden=YES;//此时按钮被隐藏
bt.hidden=NO;//此时按钮被显示在视图中
//设置UIButton的坐标和大小
[bt setFrame:CGRectMake(100, 100, 100, 40)];
//设置UIButton的颜色
[bt setBackgroundColor:[UIColor redColor]];
//设置UIButton的标题字,forState设置的是按钮的当前状态,此时为正常状态
[bt setTitle:@"aaa" forState:UIControlStateNormal];
//UIButton的各种状态
//正常状态 UIControlStateNormal;
//高亮状态 UIControlStateHighlighted;
//禁用状态UIControlStateDisabled;
//选中状态 UIControlStateSelected;
//代理状态 UIControlStateApplication;
//保留状态 UIControlStateReserved;
//设置UIButton在正常状态下的字体颜色
[bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置UIButton的标题对齐方式
[bt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];//左对齐
[bt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];//右对齐
[bt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];//居中对齐
[bt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentFill];//填充对齐
//把UIButton添加到视图
[self.window addSubview:bt];
//释放UIButton对象
[bt release];
//这里创建一个圆角矩形的按钮
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// 能够定义的button类型有以下6种,
// typedef enum {
// UIButtonTypeCustom = 0, 自定义风格
// UIButtonTypeRoundedRect, 圆角矩形
// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
// UIButtonTypeContactAdd, 十字加号按钮
// } UIButtonType;
//给定button在view上的位置
button1.frame = CGRectMake(20, 20, 280, 40);
//button背景色
button1.backgroundColor = [UIColor clearColor];
//设置button填充图片
//[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
//设置button标题
[button1 setTitle:@"点击" forState:UIControlStateNormal];
/* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//以下是几种状态
// enum {
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
// };
/*
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
* 那么可以去掉这个功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
button1.showsTouchWhenHighlighted = YES;
/* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
也可以传入其他类的指针*/
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
//显示控件
[self.view addSubview:button1];
注意:
[button1 addTarget:self
action:@selector(alarmTimeDone:)
forControlEvents:UIControlEventTouchUpInside
];
addTarget:self 是链接到self,一般都这样设置
action:@selector(alarmTimeDone:) 时间处理函数
forControlEvents:UIControlEventTouchUpInside 控件事件处理的消息
//取消按钮已经添加的所有事件:(这个比较重要,若添加了两个事件 两个事件都会被触发)
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
何时释放release UIButton?
是否在dealloc中对UIButton对象进行release操作,取决于UIButton初始化的方式。
如果使用[UIButtonbuttonWithType:UIButtonTypeRoundedRect]这种方式,是不需要进行release操作的,因为这种方式是自动释放的。如果使用 [[UIButton alloc]init]的方式,则需要主动进行release释放操作。
- ios UIButton属性大全
- iOS UIButton 属性
- ios UIButton的常用属性
- iOS开发基础 :UIButton属性
- IOS UIButton的属性总结
- iOS UIButton setImageEdgeInsets||setTitleEdgeInsets属性
- IOS--UIButton的常用属性和方法
- iOS UIButton各类属性设置大全
- iOS按钮UIButton常用属性总结
- iOS UIButton中的三个EdgeInsets属性
- iOS UIButton各类属性设置大全
- iOS UIButton各类属性设置大全
- iOS UIButton各类属性设置大全2
- ios 巧用UIButton的UIControlStateSelected属性
- UIButton属性
- UIButton属性
- UIButton属性
- 【IOS】UIButton
- 单例模式
- Linux/Mac OS 常用命令记录
- 使用poi解析excel表格
- memcached在centos下自启动脚本
- iOS8开发之 PHAsset 保存图片到相册(相机胶卷) 详解二
- iOS UIButton 属性
- iOS8开发之 PHAsset 保存图片到相册(相机胶卷) 详解三
- postgresql upsert 使用范例
- 欢迎使用CSDN-markdown编辑器
- nodejs 读取图片返回给浏览器
- hdu1964Pipes【插头dp】
- Spark 配置指南
- 取条数
- iOS Bug 汇总