UIButton详细介绍及注意事项

按钮UIButton是ios开发中最常见的控件之一,下面来介绍UIButton的详细内容,及开发中需要注意的问题。

UIButton简介:

使用目标动作设计模式,target-action模式,3种回调的模式之一。

实现原理:

使用下面的方法封装,根据用户的点击移动等动作- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;创建按钮:

//构造器方法,调用该方法创建某个样式的按钮对象+ (id)buttonWithType:(UIButtonType)buttonType参数:buttonType:按钮样式枚举值:UIButtonTypeCustom = 0, 自定义风格UIButtonTypeRoundedRect, 圆角矩形 UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用UIButtonTypeInfoLight, 亮色感叹号UIButtonTypeInfoDark, 暗色感叹号UIButtonTypeContactAdd, 十字加号按钮

UIButtonTypeSystem ,系统默认样式,若使用此样式,在使用setImage时会显示异常

示例:UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

配置按钮标题//获取按钮的标题文字,只读属性@property(nonatomic, readonly, retain) UILabel *titleLabel,注意请勿直接使用titleLabel来修改title//返回在某个状态下,按钮的标题文字- (NSString *)titleForState:(UIControlState)state参数:state:控件状态枚举值:UIControlStateNormal//常规状态显现 UIControlStateHighlighted//高亮状态显现 UIControlStateDisabled//禁用的状态才会显现UIControlStateSelected//选中状态 UIControlStateApplication//当应用程序标志时 UIControlStateReserved//为内部框架预留,,可以不管他 返回值:此状态下按钮的标题文字示例:NSString *title = [button titleForState:UIControlStateNormal];//设置按钮在某个状态下的标题文字- (void)setTitle:(NSString *)titleforState:(UIControlState)state示例:[button setTitle:@"按钮" forState:UIControlStateNormal];//返回在某个状态下,按钮标题的富文本- (NSAttributedString *)attributedTitleForState:(UIControlState)state参数:state:控件的状态返回值:富文本NSAttirbutedString 为富文本,详情参见NSAttirbutedString文档示例:NSAttributedString *attributedString = [button attributedTitleForState:UIControlStateNormal];//设值按钮在某个状态下的富文本标题- (void)setAttributedTitle:(NSAttributedString *)titleforState:(UIControlState)state//返回按钮在某个状态下的标题颜色- (UIColor *)titleColorForState:(UIControlState)state参数:state:状态返回值:颜色示例:UIColor *color = [button titleColorForState:UIControlStateNormal];//设置按钮标题的颜色- (void)setTitleColor:(UIColor *)colorforState:(UIControlState)state参数:color:颜色描述对象state:状态示例:[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//返回某个状态下按钮标题的阴影颜色- (UIColor *)titleShadowColorForState:(UIControlState)state//设置某个状态下按钮标题的阴影颜色- (void)setTitleShadowColor:(UIColor *)colorforState:(UIControlState)state参数:color:颜色描述对象state:状态示例:[button setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];//标题的阴影改变时,按钮是否高亮显示。默认为NO@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted配置按钮演示//按钮高亮的情况下,图像的颜色是否要加深一点。默认是YES@property(nonatomic) BOOL adjustsImageWhenHighlighted//按钮禁用的情况下,图像的颜色是否要加深一点。默认是YES@property(nonatomic) BOOL adjustsImageWhenDisabled//按下按钮是否会发光。默认是NO@property(nonatomic) BOOL showsTouchWhenHighlighted//返回按钮在某个状态下的背景图片- (UIImage *)backgroundImageForState:(UIControlState)state参数:state:状态返回值:背景图像示例:UIImage *image = [button backgroundImageForState:UIControlStateNormal];//获取按钮的填充图片- (UIImage *)imageForState:(UIControlState)state//设置按钮的背景图片- (void)setBackgroundImage:(UIImage *)imageforState:(UIControlState)state参数:image:背景图片state:状态示例:[button setBackgroundImage:image forState:UIControlStateNormal];//设置按钮的填充图片- (void)setImage:(UIImage *)imageforState:(UIControlState)state配置按钮边框效果//设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。@property(nonatomic) UIEdgeInsets contentEdgeInsets结构体:CGFloat top, left, bottom, right;四个值,分别是上左下右//设置按钮的内部标题离按钮边缘上下左右的距离@property(nonatomic) UIEdgeInsets titleEdgeInsets//设置按钮的内部图片离按钮边缘上下左右的距离@property(nonatomic) UIEdgeInsets imageEdgeInsets获取按钮当前状态(只读)//获取按钮状态,只读属性@property(nonatomic, readonly) UIButtonType buttonType//获取按钮当前标题,只读属性@property(nonatomic, readonly, retain) NSString *currentTitle//获取按钮当前的富文本标题@property(nonatomic, readonly, retain) NSAttributedString *currentAttributedTitle//获取当前标题的颜色@property(nonatomic, readonly, retain) UIColor *currentTitleColor//获取当前标题的阴影颜色@property(nonatomic, readonly, retain) UIColor *currentTitleShadowColor//获取当前按钮的图片@property(nonatomic, readonly, retain) UIImage *currentImage//获取当前按钮的背景图片@property(nonatomic, readonly, retain) UIImage *currentBackgroundImage//获取当前按钮的图片框对象@property(nonatomic, readonly, retain) UIImageView *imageView重写绘制行为

你可以通过子类化按钮来定制属于你自己的按钮类。在子类化的时候你可以重载下面这些方法,这些方法返回CGRect结构,指明了按钮每一组成部分的边界。

要克服生活的焦虑和沮丧,得先学会做自己的主人

UIButton详细介绍及注意事项

相关文章:

你感兴趣的文章:

标签云: