3

Is there any way to change alpha of UIButton's imageview? I already tried subclassing UIButton and do below code, but it doesn't work.

self.imageView.alpha = 0.0;

I know it can be done by adding external UIImageView as a subview to UIButton but I want to know if its possible without it. Thanks!

sanjana
  • 3,034
  • 2
  • 25
  • 31
  • you can try to change the UIImage alpha instead – Igor Jan 27 '16 at 14:42
  • y cant u change the button's alpha ? – Teja Nandamuri Jan 27 '16 at 14:52
  • @Mr.T I don't want to change alpha of button. – sanjana Jan 27 '16 at 14:52
  • Afaik, the only way would be to modify the alpha of the image itself as `UIButton` resets most of it's subviews all the time. – Tobi Nary Jan 27 '16 at 14:56
  • @Igor How can I do that? UIImage doesn't have alpha property directly accessible atleast and here I am looking for some simple solution otherwise for complex solution I can add UIImageView as subview to UIButton. – sanjana Jan 27 '16 at 14:59
  • Adding an imageView yourself might be the easiest way. – Tobi Nary Jan 27 '16 at 15:10
  • @JanGreve yes thats what I concluded but I wanted to give it a try asking here. Because same thing works perfectly fine with titleLabel of UIButton. This code works fine: self.titleLabel.alpha = 0; – sanjana Jan 27 '16 at 15:11
  • @sanjana >How can I do that? http://stackoverflow.com/questions/5084845/how-to-set-the-opacity-alpha-of-a-uiimage – Igor Jan 27 '16 at 15:23
  • @Igor thanks but I prefer to add custom UIImageView as subview to UIButton than this :) – sanjana Jan 27 '16 at 15:25
  • @sanjana: you may want to select an answer and tick *This answer is useful* in order to let others know. – SwiftArchitect Jan 31 '16 at 02:14

5 Answers5

4

Probably a hacky way

for (UIView *view in myButton.subviews) {
    if ([view isKindOfClass:[UIImageView class]]) {
          view.alpha = 0.5;
    }
}
3

With subclass (will survive layout changes):

override func layoutSubviews() {
    super.layoutSubviews()
    if let imageView = imageView {
        imageView.alpha = 0.2
    }
}

Without subclass (transient, suitable to animations):

if let imageView = imageButton.imageView {
    imageView.alpha = CGFloat(sender.value)
}

Subclass Source Code

@IBDesignable class FadeButton: UIButton {

    @IBInspectable var imageAlpha: CGFloat = 1 {
        didSet {
            if let imageView = imageView {
                imageView.alpha = imageAlpha
            }
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        if let imageView = imageView {
            imageView.alpha = imageAlpha
        }
    }
}

Invoke with imageButton.imageAlpha = alpha

Demo

Fade demo

► Find this solution on GitHub and additional details on Swift Recipes.

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
1

To conclude the comments on both other answers: subclassing and/or adding another UIImageView is the simplest choice to archieve persistent, reliant behaviour.

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
0

You can do it by changing alpha of an UIImage.

See this answer for the Swift version and this one for the Obj-C version.

mixel
  • 25,177
  • 13
  • 126
  • 165
0

I needed something similar to this myself recently. I wanted to have a button where I could add an image as a background, and then have a semi-opaque colour overlay to the graphic so as to create a custom coloured image button type thing.

The way I found was to have:

  • UIImageView (to contain the image required)
  • UIView (to hold a colour with alpha=0.7ish)
  • UIButton colourless/transparent.

They all have the same size, width, position and are layered in the above order. That way you can dictate what the image will be (and its alpha if necessary), a coloured overlay on top of the image (with alpha as required), and then a transparent clickable button on top.