UIButton:
UIButton是ios中最常用的视图控件,常用来响应用户的点击事件
UIButton的类是一个UIControl子类,它实现了在触摸屏上的按钮。触摸一个按钮拦截事件和动作消息发送到目标对象.
1.创建一个按钮
UIButton*loginButton=[UIButton buttonWithType:UIButtonTypeSystem];
能够定义的button类型有以下6种,
typedef enum {
UIButtonTypeCustom = 0, 自定义风格
UIButtonTypeRoundedRect, 圆角矩形
UIButtonTypeDetailDisclosure,蓝色小箭头按钮,主要做详细说明用
UIButtonTypeInfoLight, 亮色感叹号
UIButtonTypeInfoDark, 暗色感叹号
UIButtonTypeContactAdd, 十字加号按钮
} UIButtonType;
2.设置位置和大小
loginButton.frame=CGRectMake(40, 260, 50, 20);
[button setFrame:CGRectMake(20,20,50,50)];
3.设置背景颜色 loginButton.backgroundColor=[UIColor grayColor];
[button setBackgroundColor:[UIColor blueColor]];
4.给Button设置标题
[loginButton setTitle:@"登录" forState:UIControlStateNormal];
5.给标题改颜色
[loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
6.设置字体大小
loginButton.titleLabel.font=[UIFont fontWithName:@"Arial" size:15];
7.给Button设置高亮
[loginButton setShowsTouchWhenHighlighted:YES];
8.给UIButton来一个背景图片
UIImage*image=[UIImage imageNamed:@"nlf.png"];
[loginButton setBackgroundImage:image forState:UIControlStateNormal];
9.给Button变成圆角
loginButton.layer.cornerRadius=5;
loginButton.layer.masksToBounds=YES;
10.添加或删除事件处理
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];