views:

322

answers:

1

Hi. 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  A: 

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];
Eiko
Thanks. Let met rephrase:I am doing an e-book reader app. The buttons are thumbnails of all the available pages. The button already has a background image set (the thumbnail of the page.) The user can click on the button to go to the specific page. I want to put a border around the thumbnail of the page that is currently displayed. I can do this with a solid PNG, but the PNG doesn't hide its own alpha channel. The PNG has an alpha channel (did in Photoshop) but UIImage draws it as opaque white and not transparent as a PNG should.
Flippie
I want to overlay the PNG as a separate UIImageView over the button as 2nd subview of the buttons' superview.
Flippie
Added the code for the overlay. Does this fit your needs?
Eiko
Many thanks for your help so far, but I think you are misunderstanding my question. I am already overlaying an image as a subview and it shows up fine. I just can't get the alpha channel to be transparent. The whole image is opaque but I want the PNG's alpha channel to be transparent so that I can see through to the subview. Does iPad recognise alpha channel data in PNG files and how do you turn it on?
Flippie
You can add as many subviews as you want, and alpha is taken care of automatically. Maybe post the code where you add the image and upload the pgn somewhere?
Eiko
Thanks. I realised my mistake. I was stupid and didn't double-check that my graphics program actually outputs the png with alpha correctly. I just had to change some settings in the graphics app. In my defence, I was totally new to the application. :-)
Flippie
Happens to the best of us ;-) Welcome to StackOverflow, and get in the habbit of accepting the answer. :-)
Eiko