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
Related
I have several buttons in my app that utilize images that are pictures.
For the buttons set in Interface Builder, I set the image in attributes inspector. It displays without a tint, as I desire. I checked the method used to set the image in interface builder is UIButton setImage:forState:
Now, if I try to do set the image of the button in cellForRowAtIndexPath in a UITableViewController, there's a tint over the image.
// This puts a tint over the button
customCell.myButton.setImage(buttonImage, forState: .Normal)
As a workaround, I threw an UIImageView behind the button, but when I animate the button the imageView doesn't move, which I find annoying.
I've also tried this:
// This results in a blank image on the button
customCell.myButton.imageView?.contentMode = .ScaleAspectFit
customCell.myButton.imageView?.image = buttonImage
If anyone's got suggestions re: what I'm doing wrong, I welcome your input.
It turns out imageWithRenderingMode solves the problem.
Creates and returns a new image object with the specified rendering mode.
A new image object with the specified rendering mode.
I needed to use the AlwaysOriginal case:
Always draw the original image, without treating it as a template.
let buttonImage = UIImage(named: buttonImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
Hey I want to create a checkbox in the screen, without using two different images with V and without. Does anyone know how I can do it? I didn't find UI class for it.
In your case, you can use a UIButton. Therefore having the V image will suffice. (Empty square image method is also explained)
The main method you need to call is:
//Set the 'V' image for UIControlStateSelected and [UIImage new] for UIControlStateNormal
- (void)setImage:(UIImage *)image
forState:(UIControlState)state;
//Then, for the surrounding square box you can either use an image, OR better, use the borderWidth and borderColor property of the button's layer to achieve this effect.
//Image Method
- (void)setBackgroundImage:(UIImage *)image
forState:(UIControlState)state; // Incase you use an image then, use the same image for both UIControlStateSelected and UIControlStateNormal.
//drawing method
[checkBoxButton.layer setBorderWidth:<Enter Width in CGFloat>];
[checkBoxButton.layer setBorderColor:<Enter Width in CGColor>];
NOTE: Here the crucial piece will be to maintain the selected state of the button by toggling the 'selected' property. The rest will take care of itself.
That involves manually creating a UICheckbox which draws a 2D rectangle view and a custom view for the check.
So you will have to create your own UI element.
I want to make use of the UIStepper but it's too small for my needs. That's why I found out that
[UIStepper setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
Gets the stepper as big as the picture is. The problem here is that it sets the background for both "tiles", the + and the - one.
Is there a possibility to change the background of the individual tiles?
Alternatively I could just make 2 buttons work the same way as a stepper does, but just continuing with the stepper would save me 30 minutes.
Ok I had a quick look into the .h file and found:
- (void)setDividerImage:(UIImage*)image forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
- (UIImage*)dividerImageForLeftSegmentState:(UIControlState)state rightSegmentState:(UIControlState)state NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
Which means you can pretty much do your own stile as you can put together the images of the left and the right tile in one file + set a custom divider.
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 to change the label of a UISwitch from ON-OFF to YES-NO.
I want this method to be implemented in separate class and then accessed by other classes.
I have tried to implement the snippets provided in the cook book, but without success
you can use images for on and off
#property(nonatomic, retain) UIImage *offImage;
#property(nonatomic, retain) UIImage *onImage;
image size is of 77*27
UISwitch uses images for drawing. To change the text of a UISwitch, you would have to set the onImage and offImage properties of the UISwitch to use images with your custom text. This could be done directly on a UISwitch instance, or using UIAppearance to set your custom image across all UISwitch instances in your app:
[[UISwitch appearance] setOnImage:onImage];
[[UISwitch appearance] setOffImage:offImage];
Unfortunately, setting custom on and off images for UISwitch is not functional in iOS 7 or later. From the documentation:
In iOS 7, this property has no effect.
In iOS 6, this image represents the interior contents of the switch. The image you specify is composited with the switch’s rounded bezel and thumb to create the final appearance.
And it has not been marked as deprecated. In iOS 8 this still seems to be the case, unfortunately. Customizing the colors of a UISwitch still works, but using custom images does not. To customize the images (and thus text) of a switch you will have to use a custom control class.