views:

2076

answers:

3

I thought to be clever and just put an transparent UIButton over an UIImageView with the exact frame size, so that I can wire it up easily with any event I like, for example TouchUpInside, and make it call a action method of an view controller when the user touches it. Well, it works until alpha is below 0.1f. If I do 0.01f, it will not work. So to get it work, when looking a long time on the screen, you'll see that 0.1f of alpha shining through. And that's totally disgusting ;)

It seems like iPhone OS trys to be clever and won't catch events on the button if it's visually not there. Any idea how to solve that?

Sure I could make a subclass of UIImageView and implement touchesBegan:... etc., but it doesn't feel really elegant. I mean...when I want to hyperlink an image on the web, I would never want create my own HTML element for that image, just to wire it up to an url when it's clicked. That just doesn't make a lot of sense to me.

+4  A: 

You should be able to set the button's 'Type' to Custom in Interface Builder, and it will not display any text or graphical elements over the UIImageView. This way, you don't need to adjust the alpha. If the view is built from code, use:

button.buttonType = UIButtonTypeCustom;
Jason
Perfect! I had UIButtonTypeRoundedRect. Now I don't need alpha. Great stuff!
Thanks
I get the following error when setting to UIButtonTypeCustom:"object cannot be set - either readonly property or no setter found"
Sheehan Alam
+1  A: 

i also beleive you can assign an image to a button.

The image can take up the entire frame and can also have no other artifacts of the buttone if you set it up right.

check out the Property

UIButtonInstance.currentImage

That way you are not hogging your resources with elements that are essentially already there.

Bluephlame
Would be an option too. I will try that.
Thanks
currentImage is readonly. But there is an [button setImage:image forState:UIControlStateNormal]; method. Only "bad" thing about it is, that it seems like if I had to provide an image for every state ;) ...maybe it will just pick that image for all states, if I dont provide another one. I'll try.
Thanks
A: 

In addition to UIButtonTypeCustom I set the button text colors as follows:

[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; [button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted]; [button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];

David Green