I do not remember who said, China's users are probably the world's most like the multi-skin features of users. I hate to write Android programs, graphical interface design tools and difficult to use, not as good as handwriting, editor slow like snail, smart tips always keep up with the speed I entered, the same function, Android code is at least three times times the iOS, each line of code, feel their fingers in the blood. But Android's flexible and unified style function is really intimate! Before 5, there was no better way to achieve the same functionality on the iOS platform.
After iOS5, Apple binds all the interface components to a protocol called Uiappearance, and you can simply set the overall style of the component through uiappearance.
For example, I want to set all the UIButton title to white:
[[UIButton appearance] Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
Or, I think all the UIButton have a uniform background image.
[[UIButton appearance] Setbackgroundimage:abackgroundimage Forstate:uicontrolstatenormal];
In this way, the entire application of the Uibutton,title is white, the background using abackgroundimage pictures.
Of course, the actual project, the different style of the component, should be defined as a separate class, and then in its Initialize method set its uiappearance, for example, I define a Afkbutton class, I can write the following, The button for all Afkbutton classes in this application is a style.
@implementation Afkbutton ... + (void) Initialize {if (self = = [Afkbutton self]) {[[Self appearance] Settitlecolo R:[uicolor Whitecolor] forstate:uicontrolstatenormal]; [Self appearance] setbackgroundimage:abackgroundimage Forstate:uicontrolstatenormal]; }} ... @end
To modify the uiappearance, there is a limitation, that is, if you want it to take effect, it must be loaded into the app's main window the next time, so if you want to dynamically modify the style of the component through uiappearance, we need to implement the following code in Uiappdelegate
Uiviewcontroller *rootviewcontroller = Self.window.rootViewController; Self.window.rootViewController = nil; [[UIButton appearance] Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal]; [[UIButton appearance] Setbackgroundimage:abackgroundimage Forstate:uicontrolstatenormal]; Self.window.rootViewController = Rootviewcontroller;
As described above, I designed the style of the dynamic switch skin as follows:
1. First build the corresponding component class according to style, for example, you have several buttons, you can implement several button classes on inheritance.
2. Set the global style flag.
3. Trigger the style modification where the message is sent through the global broadcast.
4.UIAppDelegate Reload Window's Rootviewcontroller
Demo Project