UIButton with changing images when pressed - but unattractive effect when changing them - iphone

I have a button that changes its image to different images depending on what modus we are in.
The images are set with:[modusBtn setImage:cx forState:UIControlStateNormal];
The images have round corners and the button is of Type "Custom".
Now when the button is pressed one can see another image in the upper and lower edge background.
Really weird - the buttons ALL have round corners - but for example when the blue modus is on and shows the blue image and one presses the blue button now - one can see red little edges only on the left two corners.
I already played around with all the button attribute settings in IB, but no luck.
Did anyone have a similar experience when changing button images?
So far all my button images where 100% rectangular and did not have round corners - therefore I never experienced this before.
Many thanks!

Use this UIButton's propriety for your button.
avatarButton.clipsToBounds = YES;

Whenever you set an image for custom button make sure to set
yourButton.backgroundColor=[UIColor clearColor];
The effect was happening as your button was using default background transitions during changing the state.

Related

Custom UIButton image buttontype

If you can set a button's image even if it's button type is rounded rectangle, then what is the purpose of the button type to begin with? I usually use the custom button type, but does this in fact actually make a difference?
You can use UIButtonType to make a button of a standard type: a detail disclosure, an info, and so on. In these cases you would not need to set the image at all - the system will do it for you automatically.
When you pick rounded rectangle and set the image, the image would be clipped to the rounded rectangle's border; when you pick custom style, there will be no clipping.
Yes this definitely makes a difference. For example, if you have two buttons, One RoundRect and the other 2nd Custom.
Try to set a background color on both buttons. You will see the difference. The Custom Button has occupied the background color properly and the round rect has few issues.
Even with background Image, you can check both. The Custom button has no issues with setting images, as it has a transparent background and the round rect will have an issue on its corners.
UIButtonType definitely have difference of this.
I hope this is what you are looking for.

How to create a gray overlay for a UIButton in touched state programmatically?

I would like to make a generic class that when tapped, makes the element grayish.
Facebook's app is the perfect example of what I want to achieve. All their links and images become gray when tapped.
I can only guess that they are subclassing UIButton.
I have made my button's style UIButtonTypeCustom to get rid of the rounded border. Beyond this, I don't know how to have the gray overlay because I see no such property in the documentation.
Its simple:
#define TAG_GRAYVIEW 5671263 // some random number
// add the gray overlay
UIView *grayView = [[UIView alloc] initWithFrame:button.bounds];
grayView.backgroundColor = [UIColor grayColor];
grayView.tag = TAG_GRAYVIEW;
[button addSubview:grayView];
// remove the gray overlay
UIView *grayView = [button viewWithTag:TAG_GRAYVIEW];
[grayView removeFromSuperview];
I think you need to use a semi transperant grey image PNG file. You need to then set Image of button in Highlighted state.
Also note that both the images for Normal State and Highlighted State need to have the images with titles on them.
As once we set the image to button, btn.titleLabel.text won't be displayed.
So you can have a image with transperant background and title on it for Normal state. And an grey image with title on it for Highlighted State.
Code for doing it programmatically is
[btn setImage:#"Transperant.png" forState:UIControlStateNormal];
[btn setImage:#"Grey.png" forState:UIControlStateHighlighted];
Hope this helps you.
The default UIButton masks the opaque content using a gray highlight. For example when using a transparent png, only the parts that contain non-transparent pixels will be grayed out on touch. The default UIButton highlight has no effect on subviews/sublayers, and will only work on provided images. What you see in the Facebook app is probably just a UIWebView highlight, possibly customized using css.
To create something similar using your own control (to prevent the overhead of a UIWebView) you should probably create your own UIControl subclass and highlight the content on touch using a transparent CALayer. You can even mask the CALayer to only highlight the actual contents, instead of a rectangular shape.
Also see my stackoverflow post on creating custom controls for iOS by subclassing UIControl.
Try setting the button up something like this.
*mind you I didn't create the image for this example.
Set your button type to custom, and in "State Config" select "Highlighted" from there you will want to set the image of the button to be a semi-transparent grey image. There are probably several other ways to achieve this same effect but this one should be nice and simple.

iPhone: How to avoid background color coming up when button is selected?

I have a button with a curved image set as its foreground image. It looks like:
Now when the button is on pressed state, it shows up like this:
notice the grey rectangular color coming up around the edges. Is there any easy way to avoid the grey background coming all over the rectangle ?
You need to set the property Shows Touch On Highlight to enabled.
Programmatically you can do that with:
[button setShowsTouchWhenHighlighted:YES];

How does iPhone draw controller?

I'm a newbie in iPhone development and now encounter a problem:
I want to add a small hotspot on a large image and simply came up with the idea that I can add a button in that area setting alpha 0 to hide the button. Unfortunately, the phone does not response click events any more. Instead, if I increase the alpha, I can receive click events without any problems. So I wonder in which order does iPhone draw controller on the screen. Is it because my background image is drawn after the button if I set alpha of button too low, so the click event is intercepted by the background image?
Any hints will be highly appreciated. Thank you all in advance!
Best Regards.
Set the button's style to Custom; the button will not be drawn, but should remain fully active.
The problem is not the order in which the views are drawn, it's rather that if a view's alpha is 0 it does not receive touch events. it's like the view is not there at all.
but you don't have to set the alpha to 0 on the UIButton, you can simply make it a Custom button instead of a Rounded Rect button. it'll be invisible and still work.

How to configure UIButton to use showsTouchWhenHighlighted?

I have a UIButton that's 90x90 in size and I would like it to glow when tapped. Using showsTouchWhenHighlighted will make it glow but it's glowing out of the center of the button and for a small radius only. This might work well for a small button like the info button, but for my button size, it's not. Is there anything I can do to make it glow from the outer perimeter of the button before I resort to designing a highlighted state?
I can think of 2 ways to do what you want:
Use different highlighted background image for your button's highlighted state,
Use CoreGraphics to draw the glow effect in your button's IBAction (touchDown is a decent place).
This is assuming you want some kind of custom glow effect of course - UIButtons already naturally have a highlighted effect when touched.