UIKit框架-04.UIButton

来源:互联网 发布:js倒计时代码 编辑:程序博客网 时间:2024/05/22 04:55

UIKit框架-04.UIButton


1.UIButton基本概念

  • UIButton是按钮控件,专门用来监听用户的点击事件,并且在用户点击后能够做出响应
  • UIButton继承UIControl,是UIControl的子控件,而UIControl继承UIView,所以UIButton最终也是继承UIView的
  • UIButton本身拥有一个Label控件,即可以设置文本,同时拥有两个UIImageView控件,可以设置图片和背景图片

UIButton属性


2.UIButton的常用属性和方法

  • 快速创建一个选定样式的按钮
/*     UIButtonTypeCustom = 0,//自定义     UIButtonTypeSystem, //默认按钮样式     UIButtonTypeDetailDisclosure,     UIButtonTypeInfoLight,     UIButtonTypeInfoDark,     UIButtonTypeContactAdd,     UIButtonTypeRoundedRect = UIButtonTypeSystem,*/UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];// 设置文本的内容错误写法❌     btn.titleLabel.text = @"按钮";这样设置我们是看不到任何内容的// 按钮有几种状态,在不同状态下可以设置不同的文本和图片//所以,我们不能直接拿到title属性进行赋值//需要用到如下方法:/*常见按钮状态:    UIControlStateNormal       = 0, //默认状态    UIControlStateHighlighted  = 1 << 0, // 高亮状态    UIControlStateDisabled     = 1 << 1, // 实效状态*///1.设置文本[btn setTitle:@"按钮" forState:UIControlStateNormal];//设置文本的字体颜色,以及按钮的背景图片和图片等属性均需要通过setXX方法,指定设置的状态//2.设置标题颜色[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//3.设置图标[btn setImage:[UIImage imageNamed:@"common_icon_check"] forState:UIControlStateNormal];//4.设置背景图片[btn setBackgroundImage:[UIImage imageNamed:@"common_button_big_blue_highlighted"] forState:UIControlStateNormal];// 5.监听按钮的点击    // Target:让谁监听按钮    // action:监听到之后需要执行的方法    // Events:事件的类型    // 规律: 只要是继承于UIControl的控件, 都可以通过addTarget来添加监听[btn addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside];
0 0
原创粉丝点击