一、使用概要

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

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. SpringCloud全家桶学习之断路器---Hystrix(五)

    目前我也在摸索着学习Spring Cloud,本节主要摸索的是服务熔断.服务降级.Hystrix服务监控. 一.Hystrix概述 (1)服务雪崩 服务雪崩:多个微服务之间调用的时候,假设微服务A调用 ...

  2. Fluent_Python_Part4面向对象,11-iface-abc,协议(接口),抽象基类

    第四部分第11章,接口:从协议到抽象基类(重点讲抽象基类) 接口就是实现特定角色的方法集合. 严格来说,协议是非正式的接口(只由文档约束),正式接口会施加限制(抽象基类对接口一致性的强制). 在Pyt ...

  3. Fluent_Python_Part4面向对象,10-seq-hacking,序列的修改、散列和切片

    第四部分第10章,序列的修改.散列和切片 中文电子书P423 这一章接第1章.第9章,以第9章定义的Vector2d类为基础,定义表示多为向量的Vector类.这个类的行为与Python中标准的不可变 ...

  4. Python - 八大排序算法

    1.序言 本文使用Python实现了一些常用的排序方法.文章结构如下: 1.直接插入排序 2.希尔排序 3.冒泡排序 4.快速排序 5.简单选择排序 6.堆排序 7.归并排序 8.基数排序 上述所有的 ...

  5. Windows10+eclipse+hadoop2.7.1环境配置+wordcount-折腾笔记

    刚用Ambari搭建好Hadoop,就开始写Hello World! 一.背景 1.Hadoop版本 经查看为2.7.1           Shell   1 2 3 4 5 6 7 [root@T ...

  6. Feign代理必须加value否则启动失败

    Feign代理必须加value否则启动失败 @RequestParam(value=”xxx”)

  7. 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...

  8. Visual Studio 2017安装MSDN

      在学习Visual Studio 2017的过程中,总会遇到各种各样的难题,这时候你就会求助书或者是网上大佬们的解释,但是在看视频的过程中,我发现了MSDN这个“好东西”,就立马应用于实践,下面把 ...

  9. js里常见的三种请求方式$.ajax、$.post、$.get分析

    $.post和$.get是$.ajax的一种特殊情况: $.post和$.get请求都是异步请求,回调函数里写return来返回值是无意义的, 回调函数里对外部变量进行赋值也是无意义的. 即使是$.a ...

  10. 【Hibernate 一对多】

    OneToMany public class OneToMany { @Test public void testAdd1() { SessionFactory sessionFactory = nu ...