Adjust image on UIButton - objective-c

I have custom button, with an image that I'd like to shift down within the button. I don't want to mess with the clickable portion of the button, but just want the image moved. I've tried adjusting button.imageview.frame, but it didn't seem to do anything.
Here's my code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
UIImage *buttonOnImage = [#"tabbar_icon_0"];
[button setImage:buttonOnImage forState:UIControlStateHighlighted];
[button setImage:buttonOnImage forState:UIControlStateSelected];

UIButton has imageEdgeInsets property, which can be used to shift image inside the button.
In most cases it helps a lot. You can access it from IB (in Edge drop down choose Image and then change Inset fields), so you can try different values and see if it works for your situation.

Could you not adjust the image's size by padding it out with transparency in Photoshop?

Related

iOS UIButton with type UIButtonTypeRoundedRect shows a phantom button?

We have a very simple UIButton with two background images, whenever the button is toggled on there is small rectangle shows on the up left corner of the button. This issue occurs only if the button is UIButtonTypeRoundedRect, if I switch to UIButtonTypeCustom it works fine. Does anybody have a clue?
UIButton *mainBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
mainBtn.tag=0;
mainBtn.frame=CGRectMake(128, 8, 60.0, 60.0);
mainBtn.adjustsImageWhenHighlighted=NO;
[mainBtn setBackgroundImage:[UIImage imageNamed:#"main_tabBar_btn_main.png"] forState:UIControlStateNormal];
[mainBtn setBackgroundImage:[UIImage imageNamed:#"main_tabBar_btn_main_selected.png"] forState:UIControlStateSelected];
[tabBarView addSubview:mainBtn];
[mainBtn addTarget:self action:#selector(tabBarBtnTap:) forControlEvents:UIControlEventTouchUpInside];
try this . . . .
take a custom button , and if you want to get rounded corner's then write following code
#import <QuartzCore/QuartzCore.h>
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
Is the only reason you want to use UIButtonTypeRoundedRect in order to get the rounded corners (i.e. circle)? If so, try just using UIButtonTypeCustom, and set mainBtn.layer.cornerRadius to the radius you want.

Label on UIButton of type UIButtonTypeCustom is not displaying

I want to add a borderless UIButton to my view. Using the interface builder, I do this by dragging a Round Rect Button from the object library. Then, on the attributes inspector, change the Type to Custom and leave the title to "Button." Everything is okay with the interface builder. But, this is not the case with the programmatic approach. Programmatically, this is how I do it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
When I run the app, the button is not displayed. Maybe I'm missing something here but when I change the type to UIButtonTypeRoundRect the button is displayed. But, again, I want the button to be borderless.
Anyway, I can always use the interface builder. However, I want to understand why the programmatic approach doesn't work. So, does anyone know what the problem is?
UIButtonTypeCustom is really, actually a custom button type. There are no meaningful values setup for it by default. If you want to display it, you have to set its background color and/or its title color to a non-transparent one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[someSuperview addSubview:button];
Here i have used your code the Thing you missing is that
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Hi I was in the same situation.
I do not know why somewhere in my code there was
button.translatesAutoresizingMaskIntoConstraints = NO;
Once I've deleted that line I was able to set my button frame without any issues.
Hope this helps

UIButton with a gradient layer not highlighting

created a button with a gradient layer:
//MyGradient creates and returns gradient layers
CAGradientLayer *gradient = [MyGradient blueGradient];
//MyButton is a subclassed UIButton which adds some attributes
MyButton *testButton = [MyButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(10, 10, 150, 50);
[testButton addTarget:self action:#selector(testButton:) forControlEvents:UIControlEventTouchUpInside];
[gradient setFrame:testButton.bounds];
testButton.clipsToBounds = YES;
[testButton.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:testButton];
how can i get the image to highlight when pressed? I've found a couple of example code that subclass a UIButton and overrides drawRect: method. is there a simpler way to get the button to highlight?
Your button does not have an image - so there's nothing to highlight.
Set an image (a gradient image, if you want) as the button's background ... and even if you DON'T set an image for UIControlStateHighlighted, the button will "highlight" - basically, iOS will invert colors.
Also, if you need only horizontal / only vertical gradients, you might want to chech this function out : resizableImageWithCapInsets
Have fun !
There is no built-in way to do that. Either overload UIButton and draw the gradient yourself in drawRect based on the state of the button, or create 2 gradient images for the two states and use setBackgroundImage:image forState:UIControlStateNormal / UIControlStateHighlighted.
The easiest thing to do is to put your gradient in an image file (preferably resizable) and using code such as:
[testButton setImage:normalStateImage forState:UIControlStateNormal];
[testButton setImage:highlightStateImage forState:UIControlStateHighlighted];

How do I make a clickable UIButton with animated alpha?

I’d like to create an UIButton that “breathes”, changes its alpha from 1.0 to 0.7 and back again, repeating. The code is quite simple, but when the animation runs, the button is not clickable. Which is a pity, for a button. I can understand why the button might not be clickable when the animation changes its position, but changing alpha seems quite harmless to me. Is there a way to make the button clickable, keeping the animation and not animating by hand?
Use opacity.
UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTest.frame = CGRectMake(0, 0, 100, 100);
[btnTest setTitle:#"Breathe" forState:UIControlStateNormal];
[btnTest addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTest];
CABasicAnimation *fadeAnimation=[CABasicAnimation animationWithKeyPath:#"opacity"];
fadeAnimation.fromValue=[NSNumber numberWithFloat:0.7];
fadeAnimation.toValue=[NSNumber numberWithFloat:1.0];
fadeAnimation.duration=1.0;
fadeAnimation.repeatCount=INFINITY;
fadeAnimation.autoreverses=YES;
[btnTest.layer addAnimation:fadeAnimation forKey:#"fadeInOut"];

How to change UIButton's background image while clicking it?

I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.
Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?
Whatever, please explain how to implement this....
The best way is to use multiple images.
You can set different image to different state of a UIButton by using setImage method.
As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.
reference : UIButton States
Hope this helps.