UIButton

    UIButton *button = [UIButton buttonWithType:0];
    [self.view addSubview:button];
    button.frame = CGRectMake(20, 120, self.view.bounds.size.width - 40, 60);
    button.backgroundColor = [UIColor cyanColor];//背景颜色
    [button setTitle:@"button's title" forState:0];//设置标题
    button.titleLabel.font = [UIFont systemFontOfSize:18];//设置button文字字体大小
    [button setTitleColor:[UIColor blueColor] forState:0];//设置文字颜色
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

-(void)buttonClick:(UIButton *)sender{
    NSLog(@"%s",__func__);
}

设置按钮 左对齐、 居中、 右对齐

  button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中

 //通过设置 title 离边框偏移量 来让title调整到适当的位置
    button.contentEdgeInsets = UIEdgeInsetsMake(0/*top*/,10/*left*/, 0/*bottom*/, 0/*right*/);

设置button 圆角

 //设置button 圆角
    button.layer.cornerRadius = 12.0f;
    button.layer.masksToBounds = YES;

当tableview(collectionView )的cell中有一个button按钮 点击button获取当前点击cell的indexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identify = @"RECEIVECELLID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
        
    }
    //设置默认值按钮
    UIButton *defaultAddress = (UIButton *)[cell viewWithTag:9094];
    [defaultAddress addTarget:self action:@selector(setDefaultAddress: event:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

#pragma mark - 设置默认地址
-(void)setDefaultAddress:(UIButton *)sender event:(UIEvent *)event{
    NSSet *touchs = [event allTouches];
    UITouch *touch = [touchs anyObject];
    CGPoint touchPoint = [touch locationInView:self.tableView];
    NSIndexPath *currentIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
    _current_SelectIndex = currentIndexPath.row;
  }

当button的类型设置为 UIButtonTypeSystem
用定时器改变button的标题的时候 button的标题出现闪烁的情况 (获取验证码, 倒计时的时候用到较多)

解决办法 将 button的类型 修改成 UIButtonTypeCustom 即可!

button 设置图片的两种情况

setImage 方法设置图片不会缩放图片 有多大显示多大

setBackgroundImage 方法设置图片会自动缩放比例来适应 button的大小

    UIImage *image = [UIImage imageNamed:@"image"];
    [button setImage:image forState:0];

效果图:

Simulator Screen Shot 2016年9月1日 下午5.11.33.png

   UIImage *image = [UIImage imageNamed:@"image"];
   [button setBackgroundImage:image forState:0];

效果图:

Simulator Screen Shot 2016年9月1日 下午5.13.00.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 UIButton的父类是UIControl,UIControl的父类是UIView,UIView的父类是UI...
    guaker阅读 2,815评论 1 9
  • 一个UIButton的实例变量, 使一个按钮(button)在触摸屏上生效。一个按钮监听触摸事件,当被点击时,给目...
    wushuputi阅读 1,518评论 0 1
  • 前言:UI控件整理之UIButton 一、显示图片(复选框) UIButton *button = [UIButt...
    心如止水的鱼阅读 293评论 0 0
  • UIButton的官方文档https://developer.apple.com/reference/uikit/...
    阿斯兰iOS阅读 928评论 0 0
  • 家庭,在你的印象中是什么样的? 我记得高尔基的小说《童年》里描述过一段 印象中依稀是这样子的场景: 在昏暗的小屋里...
    艺伙阅读 546评论 1 1