UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(100, 100, 100, 30);
//设置显示文本
//因为按钮有多种状态(Normal,Highlighted,UIControlStateDisabled),需要给不同状态设置文本
//UIControlState 表示控件状态
[button setTitle:@"我是按钮,点我啊"forState:UIControlStateNormal];
[self.viewaddSubview:button];
//按钮添加点击事件处理
//参数1:传入一个对象执行, 表示那个对象处理事件,一般传入self
//参数2:传入方法的selector, 表示那个方法处理事件
//参数3:传入事件类型, 最常用TouchUpInside,参数类型UIControlEvent
//按下触发事件: UIControlEventTouchDown
[button addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
//2.常用属性
//设置文本颜色
[button setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
//设置文本字体
button.titleLabel.font = [UIFontsystemFontOfSize:12];
//设置圆角矩形按钮
button.backgroundColor = [UIColorwhiteColor];
//设置圆角大小
button.layer.cornerRadius = 10;
//设置剪切(否则有的时候设置圆角没效果)
button.clipsToBounds =YES;
//点击位置高亮效果
button.showsTouchWhenHighlighted =YES;
//禁止点击
button.enabled =NO;
self.view.backgroundColor = [UIColorlightGrayColor];
//3.图片按钮的使用
UIButton *imageButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
imageButton.frame =CGRectMake(100, 200, 150, 30);
//设置背景图片
UIImage *image = [UIImageimageNamed:@"back.png"];
[imageButton setBackgroundImage:imageforState:UIControlStateNormal];
[self.viewaddSubview:imageButton];
//设置前景(文本和图片)
[imageButton setTitle:@"图片安妮"forState:UIControlStateNormal];
[imageButton setTitleColor:[UIColorblueColor] forState:UIControlStateNormal];
[imageButton setImage:[UIImageimageNamed:@"city_select.png"]forState:UIControlStateNormal];
[imageButton addTarget:selfaction:@selector(imageBtnClick)forControlEvents:UIControlEventTouchUpInside];
//调整按钮中文本和图片位置
// top, left, bottom, right
imageButton.titleEdgeInsets =UIEdgeInsetsMake(0, -100, 0, 0);
imageButton.imageEdgeInsets =UIEdgeInsetsMake(0, 80, 0, 0);
;