Is there any way to define which area of a UIButton is clickable? In my case I would like to have PNGs with an alpha channel overlapping. These PNGs should act as buttons - but only where alpha is > 0. Is there a way?
Best
Stefan
Don't do that. According to the Apple Human Interface Guidelines, all UI elements should behave in a consistent manner.
OBShapedButton is an awesome project that will give you a button that will only respond to taps that are in the images area. I've messed with Irregularly Shaped UIButtons and I think OBShapedButton is a little more accurate. Not trying to bash on the other one at all. I've used the other and it works just fine. I've just seen a more accurate hit area with this one.
Related
I know I could simply use a regular UIButton and change the background image, but is that a good practice for custom buttons? Or is it better to draw the whole button with all the gradients/shadows/images?
Does either approach offer the same quality and performance?
I'd say that a single image is more performant and cannot cost to much memory.
If the button is supposed to look like a real button, which I assume because your are talking about gradients, shadows and all the works, then it may be advisable to set button images for normal and highlighted states.
In that case UIButton does all the work for you.
(If you feel that I missed the point of that question, then please re-phrase your question.)
I have a pretty abstract question, because I'm not sure exactly how to ask it. I have a UIImageView on one of my views. I want to have the imageview look "pressed into the superview". I'm not sure what the technical term is, but I want it to look 3D and like the imageview has been pressed into the page. Is there a way to do that?
You want it engraved into the superview? Create a "frame" decorative image in Photoshop, Gimp, … and add it to your interface. Google "photoshop engraved frame" for hundreds of results.
If I add a basic UIButton all is well, they are always easy to press every time. However, when I make a custom version using my own .png it becomes hard to trigger every time.
My custom button is a little smaller 32x32 and is mostly transparent. Could it simply be that its smaller, could the transparency be a problem..?
many thanks for any thoughts..
Maybe it is because it's smaller than 32x32, try making your transparency layer a little bigger, say 40x40 to have a bigger hit area. I mean use a bigger PNG :)
Check whether you have overhided any touches method and not called super in it...
Keep the frame of the view to 40*40 and clear the background color then set the image which is 32*32... It will work correctly
I've not much response so am adding some more info.
My buttons are not rectangular, nor organised in a grid so I need a way of creating what looks like a button (and shows that it has been pressed visually, as per a standard UIButton) but where the touchable area is different to the image area.
I am using a transparent PNG and that element works fine. I've added the buttons in Interface Builder and am wondering if that is the problem.
However, if I change imageEdgeInsets, it distorts the image display, which is obviously not what I want.
Bizarrely, if I increase the dimemsions of the button, it doesn't change the image, but if I decrease them it does.
I have tried different combinations of mode (scale to fill etc), but to no avail.
I am aware that there is an image and background image property, but in IB there is only one.
Essentially, I don't understand how the geometry works and the Apple documentation doesn't seem to help.
Surely, I can't be the only person to try to do this. Any help would be warmly welcomed.
Many thanks,
Chris.
Try setting the buttons setting to Aspect Fit. This will fill the button with your image so a smaller image than the button size would leave the space around the image.
Also set the button type to custom.
In the end, I stumbled across Ole Begemann's Non-rectangular buttons class. It just does what I need - to be able to create buttons where the touchable area follows the visible element of a non rectangular image.
#Helium3 - thanks - that allowed me to use a larger touch area, bit not a smaller one.
I have an UIButton subclass and I need its look
to be identical to an UIBarButtonItem when placed on the
classic blue tinted navigation bar.
The UIBarButtonItem has a border with a kind-of gradient,
being darker at the top and blue-ish at the bottom, which
I suspect it's done with some alpha trick. The bottom
looks recessed too.
There's also some overlay which makes the button a little bit darker
and even more when in the selected state.
Can anyone help?
The short answer is that you are going to have to spend some time learning how to draw gradients and shadows in Core Graphics.
The relevant documentation is called "Quartz 2D Programming Guide".
After you learn how to draw shadows and gradients, you are going to have to spend quite a bit of time zoomed in comparing what you are drawing vs what the button looks like.
You didn't ask, but this is what I would do:
Subclass UIBarButtonItem and add your custom functionality in there. Let UIBarButtonItem draw itself.
It might be easier for you just to use a photoshopped image. Use the image for your button. This way you don't have to worry about custom drawing code.
You can use Three20 library,
specially have a look at sample TTCatalog app.
Look at the source file "StyleTestController.m"
https://github.com/facebook/three20/blob/master/samples/TTCatalog/Classes/StyleTestController.m
I'm sure you'll find your answer here.
Cheers,