您的位置:首页 > 产品设计 > UI/UE

iOS开发中UIButton的使用方法总结

2015-01-25 19:05 459 查看
iOS开发中UIButton的使用方法总结

Button即为按钮,UIButton是app开发中使用频繁的控件之一.UIButton继承于UIControl:UIView的:UIResponder:NSObject的.以下是我对UIButton的解析:

1). 创建一个button的两种方法

1. 类方法 initWithType

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

风格有如下

typedef enum {

UIButtonTypeCustom = 0, //no button type 自定义,无风格

UIButtonTypeRoundedRect, // 白色圆角矩形

UIButtonTypeDetailDisclosure, // 蓝色的披露按钮,可放在任何文字旁

UIButtonTypeInfoLight, // 微件使用的小圆圈信息按钮,可以放在任何文字旁

UIButtonTypeInfoDark, // 白色背景下使用的深色圆圈信息按钮

UIButtonTypeContactAdd, // 蓝色加号(+)按钮,可以放在任何文字旁

} UIButtonType;

2. initWithFrame

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];

// 这种方法直接初始化了button的Frame属性,完成操作button操作后 需要 [button release]; 来保持引用 计数平衡.

2). UIButton的属性及方法

1.Frame属性,指定button 的位置和大小

button.frame = CGRectMake(20, 20, 50, 50);

2.给button添加背景颜色

button.backgroundColor = [UIColor clearColor];

3.给button设置标签,用来辨别点击的是哪个button

button.tag = 0;

4.定义按钮标题

[button setTitle:@“点击”forState:UIControlStateNormal];

5.定义按钮标题字体格式

[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];

6.将button加入视图当中

[view addSubview:button];

7.给button添加点击触发事件

[button addTarget:self action:@selector(buttonAction:) forControlEvents :UIControl EventTouchUp Inside];

- (void)buttonAction:(id)sender

{

UIButton * button = (UIButton *)sender;

// 开始写自己的行为

}

8.将button加入视图当中

[view addSubview:button];

9.给button添加图片

[button setImage:[UIImage imageNamed:@“a.jpg”] forState:UIControlStateNormal];

10.设置标题颜色

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

11.阴影

[button setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];

上面三个方法都提到共同的参数 forState .这个参数决定了标题、图像或其他属性将在何种状态下显现:

enum{
UIControlStateNormal = 0, //常态

UIControlStateHighlighted = 1 << 0, // 高亮

UIControlStateDisabled = 1 << 1, //禁用

UIControlStateSelected = 1 << 2, //
选中
UIControlStateApplication = 0x00FF0000, //
当应用程序标志使用时
UIControlStateReserved = 0xFF000000 //
为内部框架预留的
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐