UI组件之Button

UIButton:按钮,可以实现用户和app的交互,父类是UIControl,事件驱动型的组件的父类都是UIControl.一般使用类方法创建一个对象,创建时指定button的类型,

 iOS7.0后采用UIButtonTypeRoundedRect没有圆角的效果

 

UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];

 

设置正常状态UIControlStateNormal下的标题,按钮一般使用选中状态UIControlStateSelected,高亮状态UIControlStateHighLighted

[button setTitle:@"click" forState:UIControlStateNormal];

 

设置某种状态下标题的颜色(一般正常状态)

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

 

选中状态时的标题颜色

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

 

设置为选中状态

button.selected=YES;

 

给按钮添加一个监听器对象self

 

当用户按下并松开(UIControlEventTouchUpInside)事件发生时,由self调用action的方法响应处理

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

 

UIControlEventTouchDown:当按钮按下未松开时的事件

[button addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];

注:addTarget: 所跟参数为一个类名,给类中存放@selector()中调用的方法

 

设置某种状态下标题的颜色(一般正常状态)

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

 

选中状态时的标题颜色

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

 

自定义button时,继承UIButton,并选择重写以下方法

- (CGRect)backgroundRectForBounds:(CGRect)bounds;

- (CGRect)contentRectForBounds:(CGRect)bounds;            

- (CGRect)titleRectForContentRect:(CGRect)contentRect;    //标题在按钮中的显示位置和大小

- (CGRect)imageRectForContentRect:(CGRect)contentRect;    //图片在按钮中的显示位置和大小

 

参数sender是发生事件的对象

-(void)buttonClick:(UIButton *)sender

{

    获取button当前的标题:sender.currentTitle

    获取button上的标题:sender.titleLabel.text

    获取button某种状态下的标题:[sender titleForState:UIControlStateNormal]

}

转载于:https://www.cnblogs.com/PaulpauL/p/4796351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值