IOS控件布局之UIButton使用

前言:

   UIButton在平时开发中使用频率比较高,虽然这个知识点很不值得一提,现在我很有当前刚开始做Android的那种感觉,心中涌起一句话:不积跬步无以至千里 不积小流无以成江海!现在开始吧,UIButton使用总结。

UIButton初始化

  UIButton *btn=[[UIButton alloc]init];//一般初始化
    
  UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];//直接指定Type的方式
    
  UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 25, 45, 45)];//指定位置和大小的方式

关于UIButtonType枚举类型

  • UIButtonTypeCustom = 0, // 自定义类型

  • UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 系统类型

  • UIButtonTypeDetailDisclosure,//详细描述样式,圆圈中间加个i

  • UIButtonTypeInfoLight, //浅色的详细描述样式

  • UIButtonTypeInfoDark,//深色的详细描述样式

  • UIButtonTypeContactAdd,//加号样式

  • UIButtonTypeRoundedRect,   // 圆角矩形

设置UIButton背景色、位置、大小

 btn.backgroundColor=[UIColor blueColor];
 btn.frame=CGRectMake(10, 25, 45, 45);

UIButton设置文字

btn.titleLabel.text=@"返回";

作为小白我第一次是这么写的,但是这样是没有效果的,正确的方式是下面的这种方式

 [btn setTitle:@"返回" forState:UIControlStateNormal];

文字标题是基于UIControlState的,UIControlState也是一个枚举类型

  • UIControlStateNormal //正常状态下

  • UIControlStateHighlighted //高亮状态下,按钮按下还未抬起的时候

  • UIControlStateDisabled  //按钮禁用状态下,不能使用

  • UIControlStateSelected  //选中状态下

  • UIControlStateApplication //当应用程序标志时

  • UIControlStateReserved  //为内部框架预留

修改文字标题属性,比如字体颜色、大小等

    btn.titleLabel.font=[UIFont systemFontOfSize:15];
    btn.titleLabel.textColor=[UIColor whiteColor];
//  btn.tintColor=[UIColor whiteColor];
//  [btn setTintColor:[UIColor whiteColor]];

UIButton设置图片

btn.imageView.image=[UIImage imageNamed:@"work_answer_edit_wrong"];

和上面设置文字是一样也是无效的,设置图片也是采用基于状态的

[btn setImage:[UIImage imageNamed:@"work_answer_edit_wrong"] forState:UIControlStateNormal];

UIButton实现图文混合

  哈哈,这点我让我想起了Android的自定义控件了,UIButton本质上也是继承UIView的,是UIView就可以使用addSubview 添加子控件进来,这点在iOS上太牛叉了,Android上只有ViewGroup才能添加子控件。这里比如实现一个上下结构图文按钮。

 UIImageView *topImageView=[[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 45, 45)];
    topImageView.image=[UIImage imageNamed:@"work_answer_edit_wrong"];
    [btn addSubview:topImageView];
    
    UILabel *bottomLabel=[[UILabel alloc]initWithFrame:CGRectMake(22.5, 45, 30, 30)];
    bottomLabel.font=[UIFont systemFontOfSize:12];
    bottomLabel.textColor=[UIColor whiteColor];
    bottomLabel.textAlignment=NSTextAlignmentCenter;
    bottomLabel.text=@"返回";
    [btn addSubview:bottomLabel];

UIButton添加事件

ios的事件处理是基于Action-Target(目标-动作)模式

一种方式不带:就不传递参数

[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

带:传递参数

[btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

所对应的action也有一点小小的区别 也是带参数和不带参数区别,目前我仅仅知道这个程度,具体的差别后续再进行学习

不带参数action函数

-(void)back
{
    self.myBlock(self.str);
    [self.navigationController popViewControllerAnimated:YES];
}

带参数action函数

-(void)back:(UIButton*)sender
{
    self.myBlock(self.str);
    [self.navigationController popViewControllerAnimated:YES];
}

UIButton其他一些属性

    //默认情况下,在按钮被禁用时,图像会被画的颜色深一些。要禁用此功能,可以将这个属性设置为NO
    btn.adjustsImageWhenHighlighted = NO;
    //默认情况下,按钮在被禁用时,图像会被画的颜色淡一些。要禁用此功能,可以将这个属性设置为NO
    btn.adjustsImageWhenDisabled = NO;
    //下面的这个属性设置为yes的状态下,按钮按下会发光,这可以用于信息按钮或者有些重要的按钮
    btn.showsTouchWhenHighlighted = YES;

总结:

   简单的总结了,项目中UIButton的使用。

posted on 2017-01-14 11:12  总李写代码  阅读(317)  评论(0编辑  收藏  举报