Change Label of UISwitch - ios

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.

Related

Defining colours programmatically and using them in Interface Builder

I want to customize the colours of an App reusing the same elements but changing the colours.
These elements are simple Views, UIButton, UILabel, etc. nothing fancy. The colours may come from a XML or plist I will preload and parse to UIColor.
Yes, I can create an outlet from each of them and set them manually, but I got hundreds of elements and I want to avoid this path.
I tried IBInspectable, without luck, I’m looking for a widespread solution, all views, all VCs.
I am coding in Objective-C by the way...
Could you suggest any approach on how should I do this?
Comment if you want more detailing…
Thank you all very much!
Couple options...
Subclass your UI elements, and use MYUIButton instead of UIButton, for example, or
Look at UIAppearance proxy - https://developer.apple.com/reference/uikit/uiappearance - Using that, you can set default appearance characteristics for the whole app.
Example:
[[UIButton appearance] setBackgroundColor:[UIColor yellowColor]];
[[UILabel appearance] setBackgroundColor:[UIColor orangeColor]];
If you include those two lines (often done in AppDelegate / didFinishLaunchingWithOptions), every UIButton in your app will have a yellow background, and every UILabel will have an orange background.
(However, you will not see these changes in Interface Builder)
Edit:
As Charles mentions in his comment, you can create subclasses and then apply appearance changes to only those classes.
Suppose you have 3 button "types" that you want to apply a "color scheme" to - dark-blue, medium-blue, light-blue or dark-red, medium-red, light-red, etc. You could take the approach of creating DarkUIButton, MediumUIButton and LightUIButton subclasses. Then, when you load your color scheme...
[[DarkUIButton appearance] setBackgroundColor:scheme.darkcolor];
[[MediumUIButton appearance] setBackgroundColor:scheme.mediumcolor];
[[LightUIButton appearance] setBackgroundColor:scheme.lightcolor];

UIButton backgroundImage versus image

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

Using custom UISlider subclass to change appearance of MPVolumeView

I created a subclass of UISlider called VolumeSlider, with the intent being to just increase the thickness of the track and reduce the size of the thumb (similar to the appearance of the default volume view in the music player). I don't want to use custom images, as the default iOS 7 slider has the basic appearance I want. In my subclass, I'm overriding the following 2 methods in order to achieve this:
- (CGRect)trackRectForBounds:(CGRect)bounds
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
My question is: How can I apply this UISlider subclass to my MPVolumeView?

UISwitch with custom image

I need to customize UISwitch by changing background color as well as knob(holder) image as shown in below image.Is it possible to customize UISwitch without app getting rejected.
Thanks in advance
There's an onTintColor property you can set on UISwitch. If you need to change the handle image as well, have a look at DCRoundSwitch.

Some UI elements don't acquire UIAppearance traits

I am trying to use UIAppearance to get a uniform color theme in my iOS app. For example I try to set the text color of all UILabel objects as follows:
[[UILabel appearance] setTextColor:[UIColor colorWithRed:0.7 green:0.07 blue:0.12 alpha:1]];
This works fine for all objects statically defined in my storyboard/XIBs. However, sometimes I need to dynamically create a UILabel in a view. In these cases, the UIAppearance is not used. Instead the default text color (black) is used instead.
Has anyone run into this issue/ found a way around it other than resorting to the old "set every element manually" approach?
Seems that not all the classes support UIAppearance and UILabel is not one of those.
Check this question for more info:
UIAppearance not taking effect on UILabels created programatically
Here's a list of classes that support UIAppearance:
http://blog.mosheberman.com/list-of-classes-that-support-uiappearance-in-ios-5/

Resources