一、使用概要

当添加一个按钮到你的界面,执行以下步骤:

1、在创建时设置按钮的类型。

2、提供一个标题字符串或图像,为您的内容适当调整按钮的大小。

3、连接一个或多个操作按钮的方法。

4、设置自动布局规则界面中的按钮的大小和位置。

5、提供可访问性信息和本地化字符串。

二、具体使用

1、使用类方法创建一个按钮对象

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

注意:能够定义的button类型有以下6种

typedef NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom = 0, 自定义风格
    UIButtonTypeSystem,标准的系统按钮
    UIButtonTypeDetailDisclosure,蓝色小箭头按钮,主要做详细说明用
    UIButtonTypeInfoLight,亮色的感叹号
    UIButtonTypeInfoDark,暗色的感叹号
    UIButtonTypeContactAdd,十字加号按钮
    UIButtonTypeRoundedRect = UIButtonTypeSystem,圆角矩形按钮
};

注意:若要设置按钮的image或者backgroundImage,建议定义按钮的风格为UIButtonTypeCustom,这样才能保证设置成功。


2、设置按钮的标题

[button1 setTitle:@"dianji" forState:UIControlStateNormal];

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

[button1 setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];


3、设置按钮的背景色、背景图片

[button1 setBackgroundColor:[UIColor redColor]];

[button1 setBackgroundImage: [UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];


4、设置按钮的图片

[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];

注意:setTitle和setImage方法不能同时使用,同时使用只会显示图片不会显示文字。如果要设置图片和标题,应该使用setBackgroundImage和setTitle或者自定义一个UIButton的子类来实现。


5、设置按钮在父件上的位置与自身大小

[button1 setFrame:CGRectMake:(100, 100, 200, 200)];

注意:按钮有以下几种状态

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal = 0,常规状态显示
    UIControlStateHighlighted  = 1 << 0,高亮状态显示
    UIControlStateDisabled = 1 << 1,禁用状态才会显示
    UIControlStateSelected = 1 << 2,选中状态显示
    UIControlStateFocused = 1 << 3,只适用于有压力感应的屏幕
    UIControlStateApplication = 0x00FF0000,当应用程序被标示时
    UIControlStateReserved = 0xFF000000,系统预留
};

关于按钮状态的几点说明:

1)默认情况下,当按钮处于高亮状态时,图片的颜色会画的深一点,如果下面这个属性设置为NO,那么这个功能无效。

button1.adjustsImageWhenHighlighted = NO;

2)默认情况下,当按钮处于禁用状态时,图片的颜色会画的深一点,如果下面这个属性设置为NO,那么这个功能无效。

button1.adjustsImageWhenDisabled = NO;

3)下面这个属性设置为yes的状态,按钮在高亮状态时会发光。

button1.showsTouchWhenHighlighted = YES;

4)设置按钮禁用

button1.enabled = NO;

5)设置按钮选中状态

button1.selected = YES;


6、连接一个或多个操作按钮的方法

[button1 addTarget:self action:@selector(btnClientAction) forControlEvents:UIControlEventTouchUpInside];

可以翻译为:当button1按钮上发生UIControlEventTouchUpInside点击事件时,会出发self(当前类)中的btnClientAction方法。

注意1、点击事件的类型有以下种类

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown = 1 <<  0, 用户按下时触发
    UIControlEventTouchDownRepeat = 1 <<  1, 点击次数大于1时触发
    UIControlEventTouchDragInside = 1 <<  2, 触摸后在控件内拖动时触发
    UIControlEventTouchDragOutside = 1 <<  3, 触摸后在控件外拖动时触发
    UIControlEventTouchDragEnter = 1 <<  4, 触摸后从控件外拖动到内部时触发
    UIControlEventTouchDragExit = 1 <<  5, 触摸后从控件内拖动到外部时触发
    UIControlEventTouchUpInside = 1 <<  6, 触摸后在控件内部抬起时触发
    UIControlEventTouchUpOutside = 1 <<  7, 触摸后在控件外部抬起时触发
    UIControlEventTouchCancel = 1 <<  8, 触摸取消事件,设备被锁上或者电话呼叫打断

UIControlEventValueChanged = 1 << 12, 当控件的值发生改变时触发
    UIControlEventPrimaryActionTriggered = 1 << 13,     // semantic action: for buttons, etc.

UIControlEventEditingDidBegin = 1 << 16, 文本控件开始编辑时触发
    UIControlEventEditingChanged = 1 << 17, 文本控件的文本改变
    UIControlEventEditingDidEnd = 1 << 18, 文本控件结束编辑时触发
    UIControlEventEditingDidEndOnExit = 1 << 19, 文本控件内通过按下回车键结束编辑时触发

UIControlEventAllTouchEvents = 0x00000FFF,  所有的触摸事件
    UIControlEventAllEditingEvents = 0x000F0000,  //UITextField编辑的所有事件
    UIControlEventApplicationReserved = 0x0F000000,  // range available for application use
    UIControlEventSystemReserved = 0xF0000000,  // range reserved for internal framework use
    UIControlEventAllEvents = 0xFFFFFFFF,所有事件
};

注意2、触发的方法带参数的含义是,将button1对象作为参数传递给触发方法。


7、取消按钮已经添加的所有事件

[button1 removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

前两个参数传nil,会使得取消button1所有的UIControlEventTouchUpInside事件触发。


8、调整按钮中的文本框和图片的位置

button1.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
button1.titleEdgeInsets = UIEdgeInsetsMake(50, 50, 50, 50);

但是,上面方法是以它们之前的位置而言的,所以为了省事,在需要调整位置的情况下使用下面两行代码,先让文本框和图片位于按钮的左上角后,再去调节按钮上文本框和图片的偏移量。

button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button1.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;


9、通过block自定义UIButton的点击事件

步骤1、自定义一个继承于UIButton的按钮

步骤2、声明block

typedef void (^ButtonBlock)(UIButton *);
@interface MyButton : UIButton

@property(nonatomic,copy)ButtonBlock block;
- (void)addTapBlock:(ButtonBlock)block;
@end

步骤3、重写button的点击事件的方法

- (void)addTapBlock:(ButtonBlock)block
{
    _block = block;
    [self addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonAction:(UIButton *)button
{
    _block(button);
}

步骤4、在视图控制器中调用button的block方法

[button addTapBlock:^(UIButton *button){
    NSLog(@"按钮被点击了");
}];

三、代码Copy使用

- (void)setupBtnObject
{
//创建一个UIButton对象
UIButton *buttonObj = [UIButton buttonWithType:UIButtonTypeCustom]; //按钮对象的标识
[buttonObj setTag:]; //按钮的透明度。0.0~1.0
[buttonObj setAlpha:0.8]; //文字的内容
[buttonObj setTitle:@"Normal" forState:UIControlStateNormal];
[buttonObj setTitle:@"Selected" forState:UIControlStateSelected];
//文字的颜色
[buttonObj setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[buttonObj setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//文字的阴影颜色
[buttonObj setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal];
[buttonObj setTitleShadowColor:[UIColor yellowColor] forState:UIControlStateSelected];
//文字的字体大小
[buttonObj.titleLabel setFont:[UIFont systemFontOfSize:]]; //图片的内容。若同时使用setTitle和setImage,只会显示图片。若想同时显示,需使用setBackgroundImage或者自定义一个UIButton类。
[buttonObj setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[buttonObj setImage:[UIImage imageNamed:@""] forState:UIControlStateSelected]; //背景色
[buttonObj setBackgroundColor:[UIColor lightGrayColor]]; //背景图片
[buttonObj setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[buttonObj setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateSelected]; //调整图片和文字的位置
buttonObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
buttonObj.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
buttonObj.imageEdgeInsets = UIEdgeInsetsMake(, , , );
buttonObj.titleEdgeInsets = UIEdgeInsetsMake(, , , ); //设置圆角
[buttonObj.layer setMasksToBounds:YES];
[buttonObj.layer setCornerRadius:6.0];
[buttonObj.layer setBorderWidth:1.0];
[buttonObj.layer setBorderColor:[[UIColor whiteColor] CGColor]]; //按钮高亮时,图片是否颜色变深
[buttonObj setAdjustsImageWhenHighlighted:NO];
//按钮禁用时,图片是否颜色变淡
[buttonObj setAdjustsImageWhenDisabled:NO];
//按钮按下时,按钮是否发光
[buttonObj setShowsTouchWhenHighlighted:NO]; //添加事件
[buttonObj addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; //禁用按钮
[buttonObj setEnabled:NO];
//按钮选中
[buttonObj setSelected:YES];
//若为NO,则发生在按钮上的事件传递到下一个能处理事件的控件区处理。
[buttonObj setUserInteractionEnabled:YES]; //添加显示
[self.view addSubview:buttonObj]; //设置约束条件
[buttonObj mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo();
make.top.mas_equalTo();
make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, ));
}];
}
- (void)btnAction:(UIButton *)btn
{
//取消按钮已经添加的某某事件
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchDragInside];
}

按钮UIButton的使用的更多相关文章

  1. IOS之UI -- 按钮UIButton的细节

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. iOS上如何让按钮(UIbutton)文本左对齐展示

    // button.titleLabel.textAlignment = NSTextAlignmentLeft; 这句无效 button.contentHorizontalAlignment = U ...

  3. ios 学习笔记(8) 控件 按钮(UIButton)的使用方法

    在实际开发中,对于开发者来说,更多的还是使用“自定义”按钮.将“按钮”对象的类型设置成UIButtonTypeCustom.这样一来,按钮的所有元素都将由开发者来配置和自定义. 对于一个自定义按钮来说 ...

  4. IOS UI-Button按钮

    #import "ViewController.h" @interface ViewController () { UILabel *lable; BOOL moveFlag; N ...

  5. iOS 封装添加按钮的方法

    添加按钮 #pragma mark 添加按钮 - (void)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage ...

  6. IOS UIButton用法详解

    这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.   //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...

  7. iOS 自定义NavigationBar右侧按钮rightBarButtonItem--button

    //两个按钮的父类view UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(, , , )]; //历史浏览按钮 ...

  8. iOS 自定义返回按钮,保留系统滑动返回

    原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...

  9. UI里的UIActionSheet按钮

    1.效果图:分别为有短信分享                                      无短信分享 -(void)viewDidLoad{ //添加按钮 UIButton *share ...

随机推荐

  1. python调用c++开发的动态库

    此处列举一下python调用Windows端动态库. # *- coding=utf-8 -* import ctypes from ctypes import * import os objdll ...

  2. CSS 绝对定位时,水平居中而不影响原文档中其它元素

    div.absolutemiddle { position: absolute; left: 50%; transform: translate(-50%); /* 平移50%为自身尺寸的一半,实现水 ...

  3. @ModelAttribute与@RequestBody的区别

    一.@ModelAttribute与@RequestBody的区别 @ModelAttribute与@RequestBody都是用来注解解析前端发来数据,并自动对应到所定义的字段名称. 这里先放结论, ...

  4. Spring Boot 使用 Dom4j XStream 操作 Xml

    Xml 现在仍然占据着比较重要的地位,比如微信接口中使用了 Xml 进行消息的定义.本章重点讨论 Xml 的新建.编辑.查找.转化,可以这么理解,本章是使用了 dom4j.xstream 也是在开发者 ...

  5. 为什么ISR4K、ASR1K等设备的QoS ACL没有显示计数?

    思科的ISR4K和ASR1K设备都是IOS XE的架构,它们和传统的IOS架构是不一样的. 以ISR4K为例,和一般的IOS(例如ISR G2)有所区别,他的转发更依赖硬件完成,针对NAT或QoS应用 ...

  6. 加载Properties配置文件

    /** * 加载Properties配置文件 * * @author ZhangHaiNing * @param file 要读取的文件 * @return */ public static Prop ...

  7. 【PAT甲级】1042 Shuffling Machine (20 分)

    题意: 输入洗牌次数K(<=20),输入54张牌每次洗入的位置(不是交换的位置),输出洗好的牌. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...

  8. java 基础--8 种基本数据类型:整型、浮点型、布尔型、字符型 整型中 byte、short、int、long 的取值范围 什么是浮点型?什么是单精度和双精度?为什么不能用浮点型表示金额?

     一.8种基本数据类型(4整,2浮,1符,1布): 整型:byte(最小的数据类型).short(短整型).int(整型).long(长整型): 浮点型:float(浮点型).double(双精度浮点 ...

  9. [IDEA] Idea复制文件到项目一直updating indices的问题

    通常我们在开发JavaWeb项目的时候,都需要先将网页写好,在进行复制到web目录下,如果里面包含了很多的资源文件,就会造成一直updating indices. 方法一: 这是因为项目需要对web目 ...

  10. PAT甲级2019冬季考试题解

    A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...