iOS-UIButton

viewWithTag // 根据tag取得子视图
手势.view
superView
/取得按钮的背景图片

UIImage *image = [nextButton backgroundImageForState:UIControlStateNormal];

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.layer.borderColor = [[UIColor blackColor] CGColor];
 button.layer.borderWidth = 1.0f;




 //设置圆角矩形按钮
 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];

}
-(void)imageBtnClick
{
NSLog(@"图片按钮被点击了");
}
//事件处理方法(告诉按钮,被点了执行这个方法)
//细节: 方法有一个参数, 传入事件源
//参数可以没有
-(void)buttonClicked:(UIButton *)button
{
NSLog(@"被点了,很疼");

}

你可能感兴趣的:(iOS-UIButton)