I am trying to set the image of a UIButton I have as part of a custom UITableViewCell class to display images. The images load from a custom UIImageView class that is used to manage downloading and caching of the images. The method to assign the image to the button is contained within a delegate method for the custom UIImageView that is called when the image has finished loading:
UIImage *image = [[UIImage alloc] init];
image = ((LKCCachedImageView *)self.images[arrayIndex]).image;
[(UIButton *)self.buttons[arrayIndex] setBackgroundImage:image forState:UIControlStateNormal];
This works on the simulator but not on an actual device.
I've had some issues in the past updating the UI in delegate methods as some were not on the main thread but performing setBackgroundImage using dispatch_async on the main thread does not change the outcome.
Also, if I change setBackgroundImage to setImage the image displayed on the button is just a blue square. I'm not sure what to make of the behavior and I haven't seen anyone with anything exactly like mine.
Thanks,
Eric
Related
I have UIButtons that I was trying to set the images to aspectFit. However using Storyboards I ran into some difficulty I subclassed UIButton and programatically set the imageViews of the buttons to ScaleAspectFit
#import "UIButtonWithImageAspectFit.h"
#implementation UIButtonWithImageAspectFit
-(void)awakeFromNib{
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
#end
This worked well and the buttons images display nicely.
However, now I have a need to swap out the images of the button programatically so I tried:
[_firstLetterButton setImage:[UIImage imageNamed:#"2CeadLitir.png"]];
But this does not work so according to advise here I must use:
[_firstLetterButton setBackgroundImage:[UIImage imageNamed:#"2CeadLitir.png"] forState:UIControlStateNormal];
which adds the image as a backgroundImage to the button. Now I have to images in the button.
that is scaled nicely as an image property of the button,
and another that is not scaled nicely as a background image displayed behind.
So I returned to the subclass of UIButton UIButtonWithImageAspectFit
and tried to change it so that it sets the contentMode of the backgroundImage rather than the image
But it does not seem to have a backgroundImage property that I can access but according to the documentation it does have
currentBackgroundImage
Property
However I cannot use the same method setContentMode on that currentBackgroundImage
Any ideas how to get around this?
You need to consider this illustration from Apple web-site.
It's from Buttons tutorial. In particular this:
State
A button has four states to configure for appearance—default, highlighted, selected, and disabled.
And form
UIButton class reference
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
And here is information about different UIControlState:
All this links is must-read for beginner iOS Developer.
Actually the UIButton has a method
- (void)setImage:(UIImage *)image
forState:(UIControlState)state
so All I needed to do was set the state when setting the image on the UIButton instead of on the UIButton.imageView
To preface this question, I am extremely new to Objective-C and Xcode. I have only been learning for a week or so now but I have been stuck on this problem for a few days. I am trying to display an image in a UIButton. I have 3 to choose from, blue.png, purple.png and orange.png. No matter what I do or what image I tell Xcode to use, it will only display the blue image. I have deleted the image from the project entirely. I have "reset content and settings" in the iOS simulator. What exactly is going on here?
the.h code
IBOutlet UIButton *h1;
the.m code
UIImage *buttonImage = [UIImage imageNamed:#"purple.png"];
[h1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:h1];
No matter which image I specify within the quotes, it always uses blue.png, even when the image no longer exists in the project. Thanks for the help everybody!
You must have set blue.png as the background image in the storyboard or xib file. Write NSLog(#"%#",buttonImage) to make sure that the image is not nil. Make sure that you are entering the correct name and extension purple.png
And also, use
[h1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
instead of setImage
I think first of all you should know the difference button.image and button.backgroundImage.
Using the Image (currentImage) field, you can specify an image to
appear within the content of your button. If the button has a title,
this image appears to the left of it, and centered otherwise.
The Background (currentBackgroundImage) field allows you to specify an
image to appear behind button content and fill the entire frame of the
button.
So you can use setImage or setBackgroundImage as what you want.
I have been looking around but couldn't find anything good about this.
I would like to customize the default UIRefeshControl with different loader, etc. So far I can only change tintColor & attributedTitle properties and most code I found are just basically creating a new "pulltorefresh" effect but what I want is to just use the UIRefreshControl and customize it a bit.
Is this possible?
You can't add different loader without accessing private APIs, but you can add background image:
UIImageView *rcImageView =
[[UIImageView alloc] initWithImage:
[UIImage imageNamed: #"refreshControl.png"]];
[self.refreshControl insertSubview:rcImageView atIndex:0];
assuming self is an instance of UITableViewController subclass.
Image size you need is 320x43px (#2x 640x86px), the middle area (approximately 35px) will be covered by the loader animation.
I show application logo there...
I have been looking around but couldn't find anything good about this.
I would like to customize the default UIRefeshControl with different loader, etc. So far I can only change tintColor & attributedTitle properties and most code I found are just basically creating a new "pulltorefresh" effect but what I want is to just use the UIRefreshControl and customize it a bit.
Is this possible?
You can't add different loader without accessing private APIs, but you can add background image:
UIImageView *rcImageView =
[[UIImageView alloc] initWithImage:
[UIImage imageNamed: #"refreshControl.png"]];
[self.refreshControl insertSubview:rcImageView atIndex:0];
assuming self is an instance of UITableViewController subclass.
Image size you need is 320x43px (#2x 640x86px), the middle area (approximately 35px) will be covered by the loader animation.
I show application logo there...
I have an image that I already use im my app like this via code:
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"building" ofType:#"png"]];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
and I wanted to reuse that image in the UITabBar. There is an image field on the Attributes inspector of the UITabBar image, and when I add the image name there (either building or building.png) the image does not render, but an image outline renders.
Would anyone know why that happens and how I can get the actual image to render?
Thanks!
Here you are using an, I guess, a rather small image as a pattern to form a color. For the UITabBar you need to have images of appropriate size. UITabBar knows, as many others does, appearance methods to modify UI elements. Look for UIAppearance in the docs for more infos. As far as your question is concerned have a look for setFinishedSelectedImage:withFinishedUnselectedImage under UITabBarItem.