iOS UIButton image contentMode flickering on TouchUpInside - iphone

I have a problem with UIButton image if I set its imageView contentMode to UIViewContentModeScaleAspectFill.
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
imageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageButton setImage:[UIImage imageNamed:#"myImage.jpg"] forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(doSmth:) forControlEvents:UIControlEventTouchUpInside];
The image inside button is properly scaled to fill the whole button area.
After a click/touch on button the image gets resized (flickers) like if it's contentMode is set to UIViewContentModeScaleAspectFit.
Does anyone know how to remove this flickering when click/touch occurs?
Thanks!

Why are you setting content mode of the button's imageView? The image, you set for specified state, is always filling the size of the button. So you have to set button's frame size to the size of the image and not to set imageView contentMode
The flickering could be the higlighting of the button. If you want to set custom image for highlighting, use this code:
[imageButton setImage:buttonHightlightImage forState:UIControlStateHightlighted]

Just disable/uncheck the highlighted adjusts image in attributes inspector for imageButton

Related

Resizing UIButton on 3.5inch screen

I have two UIButtons in my app, which are custom buttons with background images, so they look like two dice. When my app is run on something not-iPhone 5 (not 4.0 inch screen) I want the to resize the buttons. I already have the code that checks for non-iPhone 5 devices(and it works), I just need to know how to change the size of the UIButtons.
I have tried lots of answers for similar questions, but so far I have not written any code that actually changes the size of my UIButtons.
I have written something like this in the "viewDidLoad" inside my "check-if-screen-is-non-iPhone-5"-statement:
button.frame = CGRectMake(90, 200, 100, 100);
The answer will depend on whether you're using Interface Builder or creating yoru button programatically. Most likely, you want to set the "content mode" of the button's imageView; then update the button's frame.
Here is an example:
UIImage *image = [UIImage imageNamed:#"close"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height);
button.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[button.imageView setContentMode:UIViewContentModeScaleAspectFit];
[button setImage:image forState:UIControlStateNormal];
Good luck.

How to fill the part of the imageview with another color

I am developing an application in which I am using one UIImageView and adding one rectangle image to that UIImageView. I am getting some float value from another calculation like 67.4 and I need to place one green image on the 67.4 part of rectangle UIImageView. So please tell me how can I fill that green image onto that UIImageView ?
You can make a "rectangle", i.e. a UIView and color it as you wish. Then add it to your image view.
UIView *greenRect = [[UIView alloc] initWithFrame:
CGRectMake(67.4, 67.4, width, height)];
greenRect.backgroundColor = [UIColor greenColor];
[imageView addSubview:greenRect];

Changing the UIButton Images get button postion changed

I am changing the UIButton image on button click but it gets postion changes from original to left side. I don't know why this is happening.
Below is the button. When I select the second button, it changes image and gets to the left side. How do I fix this error ?
Below is my code
-(IBAction)locationOneButtonAction{
UIImage *buttonImage = [UIImage imageNamed:#"radiogreen.png"];
UIImage *buttonImageOne=[UIImage imageNamed:#"radiowhite.png"];
[locationOneButton setImage:buttonImage forState:UIControlStateNormal];
[locationOneButton setImage:buttonImage forState:UIControlStateNormal];
[locationThreeButton setImage:buttonImageOne forState:UIControlStateNormal];
[locationTwoButton setImage:buttonImageOne forState:UIControlStateNormal];
[locationFourButton setImage:buttonImageOne forState:UIControlStateNormal];
[locationFiveButton setImage:buttonImageOne forState:UIControlStateNormal];
[locationSixButton setImage:buttonImageOne forState:UIControlStateNormal];
resturantLocation=#"Common Man - Bedford, MA";
}
I am not hundred percent sure about your case but it usually happen because of transparent space in both images, please overlap your images in Photoshop and make sure both images should have same margins and also should be of same size.
setImage wont stretch your image to fill frame of your button, it will set to center of button.
Make sure your image size and frame of your button is equal.
Else you can use setBackgroundImage which is will stretch your image to fill frame of button.

There's a shadow on my button

i'm creating a button programmaticly for an iPad application. when i see the button, there looks to be a shadow type thing below it. what is it and how can i get rid of it?
here is the code that creates it:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.titleLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:12];
[myButton setTitle:#"test" forState:UIControlStateNormal];
myButton.frame = CGRectMake(0, 0, self.leftScrollView.frame.size.width, 50);
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gear12.png"]];
[myButton addSubview:myImageView];
[self.leftScrollView addSubview:myButton];
UPDATE:
ok, i notice i only get that effect when its in my scrollview. if i add it to the view, no shadow effect.
the top test button, the button is a subview of the view. the bottom button is a subview of the scrollview which is a subview of the view (button/view vs button/scrollview/view).
white section is the view, grey is the scrollview/view.
UPDATE 2:
as pointed out by robmayor, UIButtons always have that double line effect, just not noticeble when the background color is white. the blue is a view and the grey is the subview scrollview.
This question is old (6 months) but i'have found a solution to delete/mask this bad effect of double lines.
[yourButton.layer setBackgroundColor: [[UIColor blackColor]CGColor]];
[yourButton.layer setBorderWidth:1.0f];
[yourButton.layer setBorderColor:[[UIColor blackColor]CGColor]];
[yourButton.layer setShadowOpacity:0.1f];
[yourButton.layer setCornerRadius:10];
UIColor selected is depending of the current background of your view.
Result :
On iPads, a rounded-rect UIButton always draws a white line along its bottom edge. You can't see that white line if the button's superview is white, but it's still there.
You have a few options:
Make the superview white. This is the easiest but you might not like the way it looks.
Make some rounded rect images in your favorite image editor. Set the button type to custom and set your rounded rect images as the button's images.
Make a subclass of UIButton and override its drawRect: method.
Set the button type to custom and use the button's layer properties (button.layer.backgroundColor, button.layer.borderColor, button.layer.borderWidth, button.layer.cornerRadius) to give the button a rounded rect appearance. You'll have to update button.layer.backgroundColor when the button is touched if you want it to turn blue like a normal one does. (Actually a normal one uses a blue gradient.)
Replace [UIButton buttonWithType:UIButtonTypeRoundedRect] with:
UIButton *myButton = [UIButton new];
or:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
You want to have your customized button. You can still make it with rounded corners if needed.

UIButton with Picture and Text

I'm trying to make a button that basically mimics the buttons seen on the iphone's home screen - it has a picture placed vertically above some text, and both are part of the button and behave appropriately (dim, clickable, etc) when highlighted.
I've tried putting the picture or the text into its own frame but that doesn't work because whichever is in the new frame doesn't dim with the rest of the button.
Using the background image property doesn't work because I want the image above the text - the area behind the text should be transparent. I would REALLY prefer a programming solution instead of going in and editing all my pictures.
Create a subclass of UIView, and have a UIImageView and UILabel as subviews, and position them appropriately using code.
You'd then have the UIImageView and UILabel as properties so that you could access them, like this:
myCustomButton.imageView.image = [UIImage imageNamed:#"..."];
myCustomButton.textLabel.text = #"...";
Or you could probably find a 3rd Party custom UIView, on the internet on github or something.
Jonathan's idea is a good one, but if you're only doing it with a couple images it's not worth the trouble. You can create a UIImage and a UILabel and then place them inside a UIButton in interface builder.
Still, it's always a good idea to learn about powerful features like subclassing.
Good luck,
Aurum Aquila
do this:
UIButton *btn_chat = [UIButton buttonWithType:UIButtonTypeCustom];
btn_chat.frame = CGRectMake(40, 50, 100, 30);//CGRectMake(20, 20, 200, 72);
UIImage *image = [UIImage imageNamed:#"chat.png"];
CGSize newSize = CGSizeMake(30, 30);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[btn_chat setImage:newImage forState:UIControlStateNormal];
btn_chat.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[btn_chat setTitle:#"Chat" forState:UIControlStateNormal];
btn_chat.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
btn_chat.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btn_chat addTarget:self action:#selector(menuSelection:) forControlEvents:UIControlEventTouchUpInside];
[btn_chat setTag:1001];
[self.view addSubview:btn_chat];
and it's done :)
Create a new initially transparent bitmap drawing context, draw your image to the top and text to the bottom. Then make a new image from this drawing context, and use that new image for the button.