PNG alpha channel on UIButton (iPad)

1.2k Views Asked by At

I am relatively new to iPad programming in cocoa touch.

I have a PNG with an alpha channel. I want to overlay it as a subview on a UIButton so that it looks as though the Button has a border around it. (Alpha channel is the centre of the image so the user must still see through it).

I can't seem to find a way to display the alpha channel correctly. The centre still displays as opaque white.

If I can't do that, how would I go about drawing a border around a UIButton? Subclass UIButton and override -drawRect? Keep in mind that the buttons are programmatically (dynamically) added, as well as the borders. (I don't want to remove the button in order to add the border.) The alpha - solution would be preferable because then I can overlay the border image on the superview (scroll view) and just set the offset to correspond to a button.

1

There are 1 best solutions below

6
On

You can add images or set images to UIButton, but as far as I know you cannot set an alpha mask to the whole thing (if I understand your question correctly). So a standard button wih a hole in it is not possible. But you can use a custom type button and set the Image to whatever image you want, s o this should give you any desired effect. You can add more subviews to it if you need to refine it later.

Edit with overlay:

    UIButton *button = [UIButton buttonWithType....]; // your ordinary button here
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"youroverlay.png"]];
    // maybe adjust your overlay position here, e.g. imageView.center = .... (use bounds of button, not its frame), or the size directly
    [button addSubview:imageView];