美文网首页IOS特技iOSiOS开发记录
iOS-如何设置按钮高亮的背景颜色

iOS-如何设置按钮高亮的背景颜色

作者: 船长_ | 来源:发表于2015-12-20 23:45 被阅读8985次
  • 分析:通过按钮的不同的点击事件状态来设置背景色,或者不同的状态设置背景图片
    • 1.通过按钮的事件来设置背景色
    • 2.通过把颜色转换为UIImage来作为按钮不同状态下的背景图片
    • 3.同方法二,直接用图片设置不同状态下的背景图片

1.通过按钮的事件来设置背景色

1.通过按钮的事件来设置背景色
- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)];
    [button1 setTitle:@"button1" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor orangeColor];
    [button1 addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
    [button1 addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
}

//  button1普通状态下的背景色
- (void)button1BackGroundNormal:(UIButton *)sender
{
    sender.backgroundColor = [UIColor orangeColor];
}

//  button1高亮状态下的背景色
- (void)button1BackGroundHighlighted:(UIButton *)sender
{
    sender.backgroundColor = [UIColor greenColor];
}

2.通过把颜色转换为UIImage来作为按钮不同状态下的背景图片

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(170, 200, 100, 50)];
    [button2 setTitle:@"button2" forState:UIControlStateNormal];
    [button2 setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateNormal];
    [button2 setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];
    [self.view addSubview:button2];
}

//  颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

相关文章

  • iOS-如何设置按钮高亮的背景颜色

    分析:通过按钮的不同的点击事件状态来设置背景色,或者不同的状态设置背景图片1.通过按钮的事件来设置背景色2.通过把...

  • 【开发工具】UIButton:按钮高亮设置

    UIButton高亮状态的效果,是通过设置高亮时的背景图片来设置的,没有设置高亮时按钮背景色的属性,这个分类就是实...

  • UIImage工具类

    平时在写一些demo的过程中,想给按钮加一些背景图片,又苦于没有美工去做切图,而只设置按钮背景颜色的话又没有高亮效...

  • 4. UIButton 按钮

    标签:按钮背景尺寸、内容偏移、按钮自带的点击效果、取消高亮 常用技巧 1.根据button设置navigation...

  • UISearchBar

    设置UISearchBar中 placeholder颜色 placeholder 输入字体颜色 背景颜色 清除按钮颜色

  • iOS 小知识

    原文https://slpowercoder.github.io/ 1、去掉按钮的高亮黑 2、设置视图属性防止背景...

  • CSS背景和精灵图

    1 背景颜色 1 如何设置标签的背景颜色2 设置父元素背景颜色会不会影响子元素背景颜色 2 背景图片 1 如何设置...

  • cell上的UIImageView点击背景颜色 消失

    cell点击处于高亮状态UIImageView未设置高亮图片【导致不显示背景颜色】特别是透明的图片 #import...

  • EditText

    Input Type 单行显示 限制输入字符 限制字符总数 设置选定文本的高亮背景颜色 设置输入框底边颜色 导入A...

  • iOS-UIButton设置高亮状态下的背景色

    UIButton一般分为高亮的普通两种状态,原生的方法可以设置这两种不同状态下的文字颜色,文字内容,背景图片,按钮...

网友评论

  • 56380f345e52:你想实现的是例如三个按钮,点击的时候将点击的按钮的状态设置为选中,并且重复点击的时候没有按钮高亮状态的困扰吧?我倒是有一个比较完美的解决办法,有需要的加我qq1214171150
  • feng_dev:很棒很6很强大
  • 羽之_HB:方法1不行,当你点击拖拽到按钮范围外后颜色依旧是高亮颜色
  • 76521e381904:哥们 你这不是高亮 不要迷惑我们
  • 与世倾听X游定终生:谢谢解决了我的问题

本文标题:iOS-如何设置按钮高亮的背景颜色

本文链接:https://www.haomeiwen.com/subject/hnvyhttx.html