您的位置:首页 > 其它

扩大Button的点击范围,setImage与setBackgroundImage区别

2016-07-30 00:00 411 查看
摘要: 实际开发中常常按钮的尺寸比较小如: 20 x 20 的按钮,不容易点击,所以要扩大点击范围,通常美工会给个20 x 20的图片,这时候扩大点击最实用的方法就是 setImage代替setBackgroundImage设置图片,setImage设置图片保持图片的原来的大小,setBackgroundImage会自动铺满按钮。

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

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

// 停止系统对图片的渲染,保持原来的颜色;

UIImage *image = [UIImage imageNamed:@"beijing@1x"];

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[button setImage:image forState:UIControlStateNormal];

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

// 给button添加一个边框,容易观察,实际开发中删去边框。


button.layer.cornerRadius = 3;

button.layer.masksToBounds = YES;

button.layer.borderWidth = 0.5;

button.layer.borderColor = [UIColor greenColor].CGColor;

[self.view addSubview:button];

// 点击边框白色区域也会相应方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息