UIButton详解

UIButton是一个标准的UIControl控件

一、创建

两种方法:

1. 常规的 initWithFrame(基本不用)

 UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];

对代码创建View(UIControl继承自UIView,所以也是view)

2. UIButton 的一个类方法(也可以说是静态方法)buttonWithType(比较常用)

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

风格有如下

typedef enum {

  UIButtonTypeCustom = 0, // no button type 自定义,无风格

  UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片

  UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁

  UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁

  UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮

  UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁

} UIButtonType;

二、设置属性

1.frame属性:第2种方法创建按钮后你可以给按钮的frame属性赋值,用一个CGRect结构设置他的位置和大小

CGRect btn2Frame = CGRectMake(10.0, 10.0, 60.0, 44.0);

btn2.frame =btn2Frame;

2. title属性:对于任何特定状态下的按钮,都可以设定该按钮该状态下的按钮标题。用setTitle 方法设置即可:

[btn1 setTitle:@"BTN1" forState:UIControlStateNormal];

你也可以为按钮的某一状态设置为图。用 setImage 即可:

[btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];

此外,你还可以为每种按钮状态设置标题的颜色和阴影,以及按钮的背景。方法 setTitleColor 和 setTitleShadowColor 都需要一个UIColor对象做参数:

[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//设置标题颜色

[btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//阴影

[btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景图像

上面几个方法都提到 共同的参数 forState . 这个参数决定了标题、图像或其他属性将在何种状态下显现。你可以编程令按钮在那个状态变化

enum {

  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 为内部框架预留的

};

typedef NSUInteger UIControlState;

你只要掌握前四种状态就好了。

当按钮高亮或者禁用,UIButton 类可以调整自己的外观,下面几个属性可以让你按照需要对按钮的外观进行微调:adjustsImageWhenHighlighted默认情况下,在按钮被禁用时,图像会被画的颜色深一些。要禁用此功能,请将这个属性设置为NO:

btn1.adjustsImageWhenHighlighted = NO;

adjustsImageWhenDisabled默认情况下,按钮在被禁用时,图像会被画的颜色淡一些。要禁用此功能,请将这个属性设置为NO:

btn1.adjustsImageWhenDisabled = NO;

复制代码

showsTouchWhenHighlighted这个属性设置为YES,可令按钮在按下时发光。这可以用于信息按钮或者有些重要的按钮:

btn1.showsTouchWhenHighlighted = YES;

三、显示控件

[self.view addSubview:btn1];

[self.view addSubview:btn2];

四、重写绘制行为

你可以通过子类化按钮来定制属于你自己的按钮类。在子类化的时候你可以重载下面这些方法,这些方法返回CGRect结构,指明了按钮每一组成部分的边界。注意:不要直接调用这些方法, 这些方法是你写给系统调用的

backgroundRectForBounds //指定背景边界

contentRectForBounds // 指定内容边界

titleRectForContentRect // 指定文字标题边界

imageRectForContentRect //指定按钮图像边界

- (CGRect)imageRectForContentRect:(CGRect)bounds{

  return CGRectMake(0.0, 0.0, 44, 44);

}

五、添加动作按钮是用来干嘛的?用来激发某个动作或事件的。那我们我们要为他添加一个动作,与 UIControl 里讲的一样:

-(void)btnPressed:(id)sender{

  UIButton* btn = (UIButton*)sender;

  //开始写你自己的动作

}

[btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

、部分属性

1. contentEdgeInsets 设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。

有些时候我们想让UIButton的title居左对齐,我们设置 
btn.textLabel.textAlignment = UITextAlignmentLeft

是没有作用的,我们需要设置 

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是问题又出来,此时文字会紧贴到左边框,我们可以设置 

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距离左边框保持10个像素的距离。 

2. titleEdgeInsets   imageEdgeInsets
UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。

(1).当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。

(2).当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。

(3).当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。

(4).想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。我测试下来,当button的contentHorizontalAlignment为居中时,偏移的距离和实际传的值有些偏差,没有发现规律,看不到源码也没在研究,但把button的contentHorizontalAlignment设为居左时,contentVerticalAlignment设为居上时,可以很方便的通过EdgeInsets改变两个子控件的位置。

3. reversesTitleShadowWhenHighlighted  确定按钮高亮时是否改变阴影的Bool值.默认时NO,当为YES时,阴影在雕刻与浮雕感之间变化(差不多就是去正常offset的相反数作为新的offset)

七、方法

//设置按钮在某个状态下的标题文字
- (void)setTitle:(NSString *)title
        forState:(UIControlState)state;
//设值按钮在某个状态下的富文本标题
- (void)setAttributedTitle:(NSAttributedString *)title
                  forState:(UIControlState)state;
//设置按钮标题的颜色
- (void)setTitleColor:(UIColor *)color
             forState:(UIControlState)state;
//设置某个状态下按钮标题的阴影颜色
- (void)setTitleShadowColor:(UIColor *)color
                   forState:(UIControlState)state;
//设置按钮的背景图片
- (void)setBackgroundImage:(UIImage *)image
                  forState:(UIControlState)state;
//设置按钮的填充图片
- (void)setImage:(UIImage *)image
        forState:(UIControlState)state;
 
 
 
 
 
原文地址:https://www.cnblogs.com/zhonghuaxiaodangjia/p/4566091.html