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 //
为内部框架预留的
};
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 //
为内部框架预留的
};
相关文章推荐
- IOS开发笔记之十九——UIButton的使用总结
- iOS开发中一些重要的系统方法使用注意事项总结
- 【iOS开发】关于UIView总结与UIButton使用
- IOS开发总结之UIButton常用属性和方法
- iOS开发 关于SEL方法使用的简单总结
- [IOS开发]自定义使用AVCaptureSession 拍照,摄像,载图总结
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- IOS开发:关于UIPickerView类的代理方法titleForRow和viewForRow的使用问题
- iOS开发之NSNotificationCenter(通知)的使用方法
- iOS开发之NSNotificationCenter(通知)的使用方法
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- 总结:ADO.NET在开发中的部分使用方法和技巧
- 【.Net Compact Framework开发】 使用 Visual Studio 对移动项目进行Unit Testing的方法总结
- cocos2d-x 中的pvr.ccz压缩图片的使用方法【iOS游戏开发征文】
- iOS 开发 中级:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem自定义方法总结
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法 推荐
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- 总结:ADO.NET在开发中的部分使用方法和技巧