码迷,mamicode.com
首页 > 其他好文 > 详细

UI组件之Button

时间:2015-09-10 00:23:19      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

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]

}

UI组件之Button

标签:

原文地址:http://www.cnblogs.com/PaulpauL/p/4796351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!