UIButton的属性和使用

UIButton的使用

1.创建和显示

文本按钮 UIButtonTypeSystem

图片按钮 UIButtonTypeCustom

系统预定义的按钮

UIButtonTypeContactAdd +号

UIButtonTypeInfoLight  i符号

注意: 按钮创建一般使用buttonWithType

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.frame = CGRectMake(100, 100, 100, 30);

设置显示文本

因为按钮有多种状态(Normal,Highlighted,UIControlStateDisabled), 需要给不同状态设置文本

UIControlState 表示控件状态

[button setTitle:@"我是按钮,点我啊" forState:UIControlStateNormal];

[self.view addSubview:button];

按钮添加点击事件处理

//参数1: 传入一个对象执行, 表示那个对象处理事件, 一般传入self

//参数2: 传入方法的selector, 表示那个方法处理事件

//参数3: 传入事件类型, 最常用TouchUpInside, 参数类型UIControlEvent

//按下触发事件: UIControlEventTouchDown

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

2.常用属性

//设置文本颜色

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

//设置文本字体

button.titleLabel.font = [UIFont systemFontOfSize:12];

//设置圆角矩形按钮

button.backgroundColor = [UIColor whiteColor];

//设置圆角大小

button.layer.cornerRadius = 10;

//设置剪切(否则有的时候设置圆角没效果)

button.clipsToBounds = YES;

//点击位置高亮效果

button.showsTouchWhenHighlighted = YES;

//禁止点击

button.enabled = NO;

self.view.backgroundColor = [UIColor lightGrayColor];

//3.图片按钮的使用

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];

imageButton.frame = CGRectMake(100, 200, 150, 30);

//设置背景图片

UIImage *image = [UIImage imageNamed:@"back.png"];

[imageButton setBackgroundImage:image forState:UIControlStateNormal];

[self.view addSubview:imageButton];

//设置前景(文本和图片)

[imageButton setTitle:@"图片安妮" forState:UIControlStateNormal];

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

[imageButton setImage:[UIImage imageNamed:@"city_select.png"] forState:UIControlStateNormal];

[imageButton addTarget:self action:@selector(imageBtnClick) forControlEvents:UIControlEventTouchUpInside];

//调整按钮中文本和图片位置

// top, left, bottom, right

imageButton.titleEdgeInsets = UIEdgeInsetsMake(0, -100, 0, 0);

imageButton.imageEdgeInsets = UIEdgeInsetsMake(0, 80, 0, 0);

}

-(void)imageBtnClick

{

NSLog(@"图片按钮被点击了");

}

//事件处理方法(告诉按钮,被点了执行这个方法)

//细节: 方法有一个参数, 传入事件源

//参数可以没有

-(void)buttonClicked:(UIButton *)button

{

NSLog(@"被点了,很疼");

}

时间: 2024-08-06 09:27:35

UIButton的属性和使用的相关文章

0821基础控件(UIButton常用属性)

一.可以通过代码的方式创建UIButton 1.通用实例化对象方法: UIButton *button = [[UIButton alloc] initWithFrame:rect]; 2.快速实例化对象方法: UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 提示: 在OC开发中,实例化任何类型的非自定义对象,都请首先尝试一下是否存在快速定义方法.如果存在快速定义方法,就尽量不要使用init之类的方法实例

简述UIButton的属性和用法

UIButton属性 1.UIButton状态: UIControlStateNormal          // 正常状态    UIControlStateHighlighted     // 高亮状态    UIControlStateDisabled        // 禁用状态     UIControlStateSelected        // 选中状态     UIControlStateApplication     //      UIControlStateReserve

UIButton各种属性

//这里创建一个圆角矩形的按钮 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //能够定义的button类型有以下6种 //typedef enum { //UIButtonTypeCustom = 0,   自定义风格 //UIButtonTypeRoundedRect,   圆角矩形 //UIButtonTypeDetailDisclosure,   蓝色小箭头按钮,主要做详细说明用 //UIBu

UIButton常见属性和方法

一.创建,两种方法: 1. 常规的 initWithFrame UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)]; 2. UIButton 的一个类方法(也可以说是静态方法)buttonWithType UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 风格有如下: typedef enum { UIButto

【iOS开发-8】UIButton类型属性简单归纳以及自定义按钮的设置

(1)UIButton类继承自UIControl,而UIControl继承自UIView,因为UIView就是个矩形区域,所以UIButton实例化的对象其实都是一个矩形,虽然有各种圆角.增加联系人.信息按钮等等,给它们加个背景它们就现形成矩形了,而且它们有个frame属性,这就是设置位置和矩形框的. (2)UIButton创建一个按钮不用实例化,也就是不用alloc和init,而是直接调用内置的几个工厂方法即可,这一点和UILabel *label1=[[UILabel alloc]init]

IOS UIButton常用属性

//1.添加按钮 UIButton *nameView=[UIButton buttonWithType:UIButtonTypeCustom]; //nameView.backgroundColor=[UIColor redColor]; [nameView setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal]; [nameView setBackground

iOS UIButton各类属性设置大全

//设置自定义的按钮 //UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom]; //设置一个圆角的按钮 UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; button1.frame=CGRectMake(80,250,250, 30);//按钮的位置坐标 [button1 setTitle:@"Button1" forStat

UIButton控件属性

UIButton控件属性: 1.UIButton状态: UIControlStateNormal // 正常状态 UIControlStateHighlighted // 高亮状态 UIControlStateDisabled // 禁用状态 UIControlStateSelected // 选中状态 UIControlStateApplication // 应用状态 UIControlStateReserved // 保留状态 2.Uibutton类型: UIButtonTypeCustom

UIButton属性

UIButton属性 1.UIButton状态: UIControlStateNormal          // 正常状态 UIControlStateHighlighted     // 高亮状态 UIControlStateDisabled        // 禁用状态 UIControlStateSelected        // 选中状态 UIControlStateApplication     // UIControlStateReserved        // 保留状态 2.