iOS一个开发系列中 - UIButton 使用摘要

时间:2023-03-09 17:32:05
iOS一个开发系列中 - UIButton 使用摘要
// 初始化button并设置类型
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 可以定义的UIButton类型有下面6种:
// typedef enum {
// UIButtonTypeCustom = 0, 自己定义风格
// UIButtonTypeRoundedRect, 圆角矩形
// UIButtonTypeDetailDisclosure, 蓝色小箭头button,主要做具体说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
// UIButtonTypeContactAdd, 十字加号button
// } UIButtonType; // 设置button大小和位置
btn.frame = CGRectMake(20, 360, 280, 45);
// 设置button背景颜色
btn.backgroundColor = [UIColor colorWithRed:254/255.0f green:254/255.0f blue:254/255.0f alpha:1.0f];
// 设置button文字
[btn setTitle:@"Normal" forState:UIControlStateNormal];
[btn setTitle:@"Pressed" forState:UIControlStateHighlighted]; // forState这个參数的作用是定义button的文字或图片在何种状态下才会显现,下面是几种状态:
// enum {
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以无论他
// }; // 设置button文字颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 设置button文字字体
[btn.titleLabel setFont:[UIFont systemFontOfSize:17]];
[btn.layer setMasksToBounds:YES];
// 设置button四个圆角半径
[btn.layer setCornerRadius:4.0];
// 设置button边框宽度
[btn.layer setBorderWidth:0.5];
// 设置button边框颜色
CGColorRef colorref = CGColorCreate(CGColorSpaceCreateDeviceRGB(),(CGFloat[]){168/255.0f, 168/255.0f, 168/255.0f, 1.0});
[btn.layer setBorderColor:colorref];
// 去除button在叠加视图中的按下延迟
tableView.delaysContentTouches = NO;
// 加入点击事件
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; // 在视图中显示button
[tableView addSubview:btn]; // button点击事件
- (void)btnAction:(id)sender
{
// do something
}

本文固定链接:http://www.itechzero.com/ios-development-series-one-uibutton-usage-summary.html。转载请注明出处。

版权声明:本文博客原创文章,博客,未经同意,不得转载。