UIButton

UIButton 按钮控件 点击按钮,会触发某个事件
1)、初始化UIButton

 UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

//这里的样式是系统自带的一个枚举(一般我们选择的样式是UIButtonTypeCustom)

typedef NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom = 0,                         // no button type
    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button

    UIButtonTypeDetailDisclosure,
    UIButtonTypeInfoLight,
    UIButtonTypeInfoDark,
    UIButtonTypeContactAdd,

    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
};

2)、设置范围

button.frame=CGRectMake(140, 400, 100, 100);

3)、为button添加一个触发方法,使点击按钮触发一个方法

[button addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];

4)、给button起个名字

[button setTitle:@"变变变" forState:UIControlStateNormal]

//这里forState也是系统自带的枚举

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,//常规状态
    UIControlStateHighlighted  = 1 << 0,    //高亮状态              // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,     //选中状态             // flag usable by app (see below)
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};

5)、设置字体颜色

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

6)、设置按钮点击成高亮状态

[button setTitle:@"变变变." forState:UIControlStateHighlighted];
//显示高亮

button.showsTouchWhenHighlighted=YES;

7)、为button按钮添加一个背景图片

[button setBackgroundImage:[UIImage imageNamed:@"头像1.PNG"] forState:UIControlStateNormal];

8)点击按钮变换图片(这里是高亮状态)(设置高亮状态下得图片)

[button setBackgroundImage:[UIImage imageNamed:@"头像2.png"] forState:UIControlStateHighlighted];

9)、判断button是否被点击了(按钮式否被选中)
//这里selected是一个BOOl值

button.selected=NO;

10)、设置button的背景颜色

  button.backgroundColor=[UIColor whiteColor];

11)、添加button到视图上

[self.view addSubview:button];

12)、触发方法

-(void)changeTitle:(UIButton *)sender
{
//判断是否是选中状态
if(sender.selected!=YES){
}else{
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ldl_csdn_ios

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值