[置顶] iOS_UIButton 实现button的内容左对齐以及图片和标题中间的间隔
2017-07-27 13:09
453 查看
要实现一个类似折叠效果的按钮 不多说 直接上代码和效果
- (void)createButton
{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 55)];
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"你到底要不要点我呀" forState:UIControlStateNormal];
button.titleLabel.textColor = [UIColor blackColor];
[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"arrow_down.png"] forState:UIControlStateNormal];
button.adjustsImageWhenHighlighted = NO;// 取消图片的高亮状态
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;// 水平左对齐
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;// 垂直居中对齐
/**
* 按照上面的操作 按钮的内容对津贴屏幕左边缘 不美观 可以添加一下代码实现间隔已达到美观
* UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
* top: 为正数:表示向下偏移 为负数:表示向上偏移
* left: 为整数:表示向右偏移 为负数:表示向左偏移
* bottom: 为整数:表示向上偏移 为负数:表示向下偏移
* right: 为整数:表示向左偏移 为负数:表示向右偏移
*
**/
button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);//
button.titleEdgeInsets = UIEdgeInsetsMake(0, button.imageView.frame.size.width, 0, 0);
[self.view addSubview:button];
}
// 点击方法
- (void)onClick:(UIButton *)sender
{
sender.selected = !sender.selected;
if (sender.selected == YES) {
sender.backgroundColor = [UIColor orangeColor];
}else{
sender.backgroundColor = [UIColor blueColor];
}
}
实现效果:
- (void)createButton
{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 55)];
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"你到底要不要点我呀" forState:UIControlStateNormal];
button.titleLabel.textColor = [UIColor blackColor];
[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"arrow_down.png"] forState:UIControlStateNormal];
button.adjustsImageWhenHighlighted = NO;// 取消图片的高亮状态
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;// 水平左对齐
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;// 垂直居中对齐
/**
* 按照上面的操作 按钮的内容对津贴屏幕左边缘 不美观 可以添加一下代码实现间隔已达到美观
* UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
* top: 为正数:表示向下偏移 为负数:表示向上偏移
* left: 为整数:表示向右偏移 为负数:表示向左偏移
* bottom: 为整数:表示向上偏移 为负数:表示向下偏移
* right: 为整数:表示向左偏移 为负数:表示向右偏移
*
**/
button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);//
button.titleEdgeInsets = UIEdgeInsetsMake(0, button.imageView.frame.size.width, 0, 0);
[self.view addSubview:button];
}
// 点击方法
- (void)onClick:(UIButton *)sender
{
sender.selected = !sender.selected;
if (sender.selected == YES) {
sender.backgroundColor = [UIColor orangeColor];
}else{
sender.backgroundColor = [UIColor blueColor];
}
}
实现效果:
相关文章推荐
- ios UIButton设置单选效果,以及同时设置图片和标题
- iOS UIButton文字和图片上下左右偏移封装,一个方法即可实现button上文字和图片不同位置的放置
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS-UIButton-设置button标题和图片位置
- 关于iOS中设置UIButton的标题(含图)左右对齐的问题
- iOS - UIButton(UIEdgeInsets) 设置button上的文字和图片
- 备忘:UIButton 的图片和标题 向左对齐
- IOS开发之UIButton同时设置图片文字以及触发按钮时文字图片的切换
- iOS 实现gif 图片效果 以及 图片不变形处理
- [置顶] iOS开发之导出ipa文件以及查看内容
- IOS开发之UIButton同时设置图片文字以及出发按钮时文字图片的切换
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片 在定义位置
- 实现表格标题和内容列对齐的内嵌滚动方法
- 28.iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片 在定义位置
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS UiButton设置button上的图片和文字共存
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- IOS总结实现UIButton的图文混排 同时显示文字和图片
- iOS开发值UIButton内容对齐方式
- iOS之推送通知-本地-服务器3.服务器端实现: 如果要编写内容提供者的推送服务程序,需要进行SSL认证编程,以及构建APNS数据包,数据包分为3个主要部分:Command(命令)、deviceTo