Using constants in Nib files - ios

I have an App with few Nibs, the Nibs are built according to a color scheme. Now I have a requirement to change the color scheme. So I need to go to each Nib, and each component in it and change its color.
I was wondering whether I can tell the Nib to read the color from a "#define" so it would be easy to make these kind of changes in the future.
Or any other way to change the Nib content not by going to each Nib and changing it but by doing it is a central place.

There is currently no way to use constants or defined symbols inside the context of a Nib file. You should consider filing an enhancement request with Apple at http://bugreporter.apple.com for this functionality.

Wouldn't be suitable to set IBOutlet for each element that required color customization and then set color in code?

Even after creating the nib with some other colour, you can still change its the background colour's value in lets say the viewDidLoad function or in any other function to the #define-ed value.

Related

iOS - Dynamic Type and Interface Builder

Trying to implement support for Dynamic Type and have an issue. I set the style I want to use on a label or something in Interface Builder. I register for the UIContentSizeCategoryDidChangeNotification, and in the handler, I set the label's font to ... what? How do I know what style to use? Shouldn't there be an accessor that lets me find that out? If not, I have to put it in 2 places, which means they'll get out of sync and I'll be annoyed. Any thoughts?
I don’t think this will satisfy you, but set the font to [UIFont preferredFontForTextStyle:UIFontTextStyleTitle2 or whatever style you set in Interface Builder.
Ignore the setting in Interface Builder. It’s not even worth setting. Interface Builder is a (mostly) static representation of the initial state of your views, but this is Dynamic Type.
You could subclass UILabel to make it dynamic, and/or join us on the dark side of setting up views in code.
Since iOS 10, there's no need to follow this rationale because the adjustsFontForContentSizeCategory property allows an automatic scaling of the font sizes according to the content type size selected in the settings.
All the text styles are well defined in the Apple reference site and their size variations as well.

How to change default System font for xib?

I've swizzled systemFontOfSize:, boldSystemFontOfSize:, and systemFontOfSize:weight: but when I set the font to "System" in a xib it still sets it to the default value. How can I change the default "System" font for xibs?
I know I swizzled these methods properly because when they are called programmatically it works as expected.
I'm not interested in a solution related to setting label appearance because I need to be able to change the font size.
In my suggest, you can also swizzled initWithCoder: for UILalbel ,UIButton ,UITextField. And modify the font according to your need in the swizzling method.

Is it possible to use references to colors instead of colors in .xib files?

When developing an iPhone app, I need to create a 'copy' of that app for various clients that, among other things, changes all the colors of the application's text, backgrounds, ...
I was wondering if it's possible to have the colors stored somewhere like:
text -> #fffffff
secondaryText -> #808080
background -> #707070
...
And then use those referencees in the xib files instead of the actual color values. I want to do this in order to avoid duplicating xib files and making them impossible to maintain.
The idea would be something similar to android's resources where you can use '#color/text' for example
You want to give single app to three clients with different look and feel. But functionally they are same. Right?
Then you can have 3 xib files for the three clients
mainView_first.xib, mainView_second.xib, mainView_third.xib. Connect their outlets to a single .h file (mainView.h).
You can do your own customization in these xib files.
You can load a file like this
mainView *main=[[mainView alloc]initWithnibName:#"nameofthexibfile"];
You can achieve this programatically -
a. create a color.plist file, in that store all the color names and values that you want to use.
b. Then create a color util class, which has the method to transform those values in UIColor object.
c. Use these methods through out your app to have background color, label color etc.

Where to place the code to change the properties of a UIView

If I'm creating a UIView programmatically and I wish to change the UIView properties (background, for example, or actually, messing with CALayers), must I place the code outside of UIView such as in the View controller? Can I put the code somewhere inside UIView?
I was checking out the CoreAnimationKioskStyleMenu example, its code is inside UIView but it's loaded from Nib and can be placed at awakeFromNib, so it doesn't seem to apply to my case.
That depends. Obviously, a good way to handle this is to use a xib file, as it is designed to hold data like this, but that isn't always the best answer for every situation.
If the view is meant to be reused frequently (like a button, or some widget) throughout the application, its best to store all that customization in a subclass of the UIView.
If its a single larger view that will always be managed by a UIViewController, you can keep some of the information in the UIViewController. However, if you end up subclassing a UIView anyway it's probably best practice to keep the data in the UIView.
As a general note, I believe its worth your time to push as much of this data into a xib using interface builder. Magic values (like colors or sizes) peppered through your code will always be a problem if you want to modify it. I have found modifying a xib to be much easier.
Actually there are some methods where you could place initialization/ customization code.
(void)willMoveToSuperview:(UIView *)newSuperview;
(void)didMoveToSuperview;
will get called as soon as u add the view as a subview to another view, at which point you already have the frame and all the properties, and you can do further customizing as you wish.
(void)layoutSubviews -- generally used for changing subviews' frames and layout organization.
Will get called each time the view needs to be redrawn by the system, or when you specifically call [self setNeedsLayout] on your UIView.
Hope this helps.

Unable to change UITableViewController color background

TitlesViewController : UITableViewController
I have the above code on iPhone development. This is associated to a xib (or nib) file. In this property file I change its background color. The problem is it does not reflect my new background color. Should I be manually loading the xib file to reflect it to my TitlesViewController?
Some quick points:
You don't change the colour of a view controller, you change the colour of a view controller's view.
A xib is an XML file that tells Xcode how to create a matching nib. Neither are property files (you are likely mixing them up with plist files).
You should be able to set the background colour for just about any UIView (or subclass thereof) in your xib file. So the question is, how were you setting it the colour, what were you expecting to happen, and what did you get?
viewcontroller.view.backgroundColor=color;

Resources