UIButton:设置图像视图的背景(不设置图像)

时间:2017-11-07 09:11:37

标签: ios uibutton uikit

如何更改UIButton上图像视图的背景颜色但不设置图像? (所以我在按钮文本旁边有一个红色方块)。

当我只是设置mybutton.imageView.backgroundColor = UIColor.red并将图片视图的约束设置为28x28px时。我没有看到红色方块。

感谢。

澄清:我想设置UIButton上的图像视图的背景颜色 - 我不想为整个按钮着色!

4 个答案:

答案 0 :(得分:1)

您将问题标记为SwiftObjective-C,所以......

在Swift中,您可以使用此扩展程序创建一个"空白"具有特定颜色的图像:

public extension UIImage {
    public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
        let rect = CGRect(origin: .zero, size: size)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        guard let cgImage = image?.cgImage else { return nil }
        self.init(cgImage: cgImage)
    }
}

然后,在按钮文字":

旁边添加一个"红色方块
    let btnImage = UIImage(color: .red, size: CGSize(width: 28, height: 28))
    btn.setImage(btnImage, for: .normal)

如果您需要在Obj-C中执行此操作,则它的过程相同:

- (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)sz {
    CGRect rect = CGRectMake(0.0f, 0.0f, sz.width, sz.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

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

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

    return image;
}

    UIImage *btnImage = [self imageWithColor:[UIColor redColor] andSize:CGSizeMake(28.0, 28.0)];
    [_btn setImage:btnImage forState:UIControlStateNormal];

注意:请确保您的UIButton类型设置为Custom,否则图片将会显示为#34;。

答案 1 :(得分:0)

Use this code.
UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(85, 290,110,20)];
button.layer.cornerRadius = ROUND_BUTTON_WIDTH_HEIGHT/2.0f;
button.backgroundColor = [UIColor redColor];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.font = [UIFont systemFontOfSize:11.0];
[button setTitle:@"Download" forState:UIControlStateNormal];
[button addTarget:self action:@selector(downloadBtnClicked:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

答案 2 :(得分:0)

您应该制作自己的自定义UIButton。另一种解决方案是尝试使用IB可用选项调整默认值。

UIButton Image + Text IOS

答案 3 :(得分:0)

UIButton * _button = [UIButton buttonWithType:UIButtonTypeCustom];

[_ button setFrame:CGRectMake(0.f,0.f,128.f,128.f)]; //为您的愿望设定值

[_ button setCenter:CGPointMake(128.f,128.f)]; //为您的愿望设定值

[_ button setClipsToBounds:false];

[_ button setBackgroundImage:[UIImage imageNamed:@" jquery-mobile-icon.png"] forState:UIControlStateNormal]; //为您的愿望设置图像名称

[_ button setTitle:@" Button" forState:UIControlStateNormal];

[_ button.titleLabel setFont:[UIFont systemFontOfSize:24.f]];

[_ button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //根据您的意愿设置颜色

[_ button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; //根据您的意愿设置颜色

[_ button setTitleEdgeInsets:UIEdgeInsetsMake(0.f,0.f,-110.f,0.f)]; //为您的愿望设定值

[_ button addTarget:self action:@selector(buttonTouchedUpInside :) forControlEvents:UIControlEventTouchUpInside]; //您可以将操作添加到按钮中,例如