gpt4 book ai didi

iphone - 带有状态的 UIButton 图像

转载 作者:行者123 更新时间:2023-11-28 18:11:16 25 4
gpt4 key购买 nike

我有 UIButton 并使用适当的代码设置它的背景图像。

`[button setImage:self.image forState:UIControlStateNormal]`

它在按钮初始化时显示正确的图像,但我想让图像在状态为 UIControlStateSelected 时消失。

我能够在选择图像时更改图像,但不知道在选择时将哪个函数或值设置为无图像是正确的。

我应该将 image alpha 设置为 0.0 吗?

谢谢!

最佳答案

使用 photoshop,创建具有透明背景的新图像。将其保存为 nope.png 文件,将其拖到您的项目中。然后

[button setImage:[UIImage imageNamed:@"nope.png"] forState:UIControlStateSelected];

另一种方式是您需要创建新的图像类别:

@interface UIImage (UIImageFrameColor)
+(UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size;
@end

@implementation UIImage (UIImageFrameColor)
+(UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, YES, 0);

[color set];
UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
[path fill];

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

return image;
}
@end

然后你需要使图像具有与按钮晚餐 View 的背景颜色相同的颜色

    UIImage *bgImage = [UIImage imageWithColor:button.supperview.backgroundColor andSize:button.frame.size];
[button setImage:bgImage forState:UIControlStateSelected];

关于iphone - 带有状态的 UIButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18392222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com