UIButton的imageView属性和隐藏/ alpha值

时间:2012-07-26 16:07:44

标签: ios uikit uibutton hidden alpha

我有一个UIButton应该在某些情况下显示活动指示器而不是图像。我现在所做的是将按钮的imageView的隐藏属性设置为YES并返回。我也尝试将alpha值设置为0.0f并返回到1.0f。

这一直有效,直到按钮状态发生变化。这会重置imageView的属性并导致隐藏== NO和alpha == 1.0f。

有没有人做过类似的事情,或者知道如何隐藏按钮的imageView,而其余部分仍然可见?

5 个答案:

答案 0 :(得分:13)

你可以通过使用视图层的transform属性来实现这一点,即

隐藏

swift code

button.imageView?.layer.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)

目标-C代码

button.imageView.layer.transform = CATransform3DMakeScale(0, 0, 0);

取消隐藏

button.imageView?.layer.transform = CATransform3DIdentity

目标-C代码

button.imageView.layer.transform = CATransform3DIdentity

答案 1 :(得分:3)

很简单。 按钮有不同的状态。只需为选定状态设置零图像,为正常状态设置图像。如果要将活动指示器设置按钮状态显示为已选择,并将指示器添加为按钮子视图。

- (void)initializeButton {
    button = [[UIButton alloc] init];
    [button addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];

    [button setImage:[[UIImage alloc] init] forState:UIControlStateSelected]; //zero image
    [button setImage:yourImage forState:UIControlStateSelected]; //your image
}

- (void)buttonDidClick:(UIButton *)button {
    button.selected = YES;
    /* Add yor activity indicator with some view tag or save reference to it */
}

活动结束后调用next方法

- (void)finishActivityWithButton:(UIButton *)button {
    button.selected = NO;
    /* Remove your activity indicator from button */
}

答案 2 :(得分:3)

如果您使用模板图像,则将色调颜色设置为[UIColor clearColor](也许应该也可以在其他图像上工作,不要尝试)

[[button imageView] setTintColor:[UIColor clearColor]];

答案 3 :(得分:1)

您可以通过在要隐藏图像时从按钮中删除图像并在需要显示图像时将图像指定回按钮来实现此目的。

如果要隐藏图像,请执行以下操作:

[yourButton setImage:nil forState:UIControlStateNormal];

当你想要显示图像时:

[yourButton setImage:yourImage forState:UIControlStateNormal];

此处yourButtonUIButtonyourImageUIImage,其中包含按钮图片。

答案 4 :(得分:0)

更改转换或选择状态将导致另一个问题,最好的方法是使用类别:

const char kNormalImage;
const char kSelectImage;
const char kDisableImage;

@interface UIButton ()

@property (nonatomic, strong) UIImage *normalImage;
@property (nonatomic, strong) UIImage *selectImage;
@property (nonatomic, strong) UIImage *disableImage;

@end

@implementation UIButton (Hidden)

- (void)setImageViewHidden:(BOOL)hidden {
    if (hidden) {
        self.normalImage = [self imageForState:UIControlStateNormal];
        self.selectImage = [self imageForState:UIControlStateSelected];
        self.disableImage = [self imageForState:UIControlStateDisabled];

        UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0.0);
        UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [self setImage:blank forState:UIControlStateNormal];
        [self setImage:blank forState:UIControlStateSelected];
        [self setImage:blank forState:UIControlStateDisabled];
    } else {
        [self setImage:self.normalImage forState:UIControlStateNormal];
        [self setImage:self.selectImage forState:UIControlStateSelected];
        [self setImage:self.disableImage forState:UIControlStateDisabled];
    }
}

- (UIImage *)normalImage {
    return objc_getAssociatedObject(self, &kNormalImage);
}

- (void)setNormalImage:(UIImage *)normalImage {
    objc_setAssociatedObject(self, &kNormalImage, normalImage,  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImage *)selectImage {
    return objc_getAssociatedObject(self, &kSelectImage);
}

- (void)setSelectImage:(UIImage *)selectImage {
    objc_setAssociatedObject(self, &kSelectImage, selectImage,  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImage *)disableImage {
    return objc_getAssociatedObject(self, &kDisableImage);
}

- (void)setDisableImage:(UIImage *)disableImage {
    objc_setAssociatedObject(self, &kDisableImage, disableImage,    OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end