IOS UIAppearance - ios

I have the following issue. I have a custom UIView which background I am trying to set using UIAppearance.
[[OptionsHeader appearance] setBackgroundColor:[UIColor headBackgroundColor]];
But when I use it like this, all the views turns to black and white.
[[UIView appearance] setBackgroundColor:[UIColor headBackgroundColor]];
Works nicely, but I need it to work nicely on that specific class.
Any help is welcome.
Kind Regards,
EZFrag

You need to implement the UIAppearance protocol in your custom class, as described here. Basically, you 'need' to augment your custom class and annotate it with UI_APPEARANCE_SELECTOR to define what (and how) the appearance delegate can interact with your class.
I say 'need' because it doesn't really seem to be required, but it's hard to be sure...

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];

How to make the background of all UITextViews Purple without subclassing?

I have an app that I want to debug. I'd like every UITextview within the app to have a purple background.
I think method swizzling is a possible solution, but I have not been able to get this to work.
Question: How can I do this without subclassing or manually setting each textview's background color?
Thank you
Something like this should do the trick:
[[UITextView appearance] setBackgroundColor:[UIColor purpleColor]];
The appearance proxy was introduced in iOS5 as a convenient way for styling core UIKit classes without subclassing. It can be used in a variety of really useful ways such as setting a UINavigationBar's tint color, button tints, etc.

When do UIAppearance proxy rules get applied to new view controller?

I'm wandering when exactly do UIAppearance rules get applied to some new view controller?
I've made some global rules and I call them in my app delegate. That way all UIButtons look that same. But now I want to modify just appearance of one UIButton. I've tried putting the code to remove it's background inside - (void)viewDidLoad but it's not working - UIAppearance rules aren't applied yet. In one ViewController I put modification code inside - (void)viewWillLayoutSubviews and it worked perfectly, but now in another ViewController it doesn't work (code is the same).
Where is it safe to override UIAppearance rules?
According to the 2011 WWDC video introducing UIAppearance, the customizations are applied right before -layoutSubviews is called on the view.
If you're looking to customise one specific button you should either:
Alter the properties of the button instance directly, and not touch the appearance settings for the class, or
Use the appearanceWhenContainedIn: method like below:
[[UIButton appearanceWhenContainedIn:[CustomView class], nil]
setBackgroundImage:myButtonImage
forState:UIControlStateNormal];
That way you can still use the appearance proxy for your specific button (when you know it's contained in a specific view), and not alter the general appearance settings.

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/

Alternatives to appearance proxy for Table Cells' UILabel setFont

I'm loving being able to use an appearance proxy in my iOS5 app to customise the NavigationController UI elements system-wide, however:
I have a lot of nested tables in my app, of which I'd love to be able to change the font across all table cells. They are mainly statically created. I've put some cell generation inside of a class, being read from an array, but the main issue lies with a large contents section I've tabled in Interface Builder. IB doesn't seem to have the option for mass-font setting.
I was wondering if anyone could help me find a way to set all the UITableCell fonts in one go?
Perhaps something like:
[[UILabel appearance] setFont:[UIFont fontWithName:#"Times" size:17.00];
You can use appearanceWhenContainedIn: to narrow down which UIViews you'd like to set the appearance for.
In your example, try:
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil]
setFont:[UIFont fontWithName:#"Times" size:17.00]];
Actually, no. It is not valid to use UIAppearance to style a UILabel. See my self-answered question here. It kinda works, but it's not valid, for the reasons outlined over there.

Resources