Customizing the default appearance of a control using appearance proxy (detailed)

Source: Internet
Author: User

The appearance of the control, which is affected by tint color,background image, background color, and so on, typically modifies the appearance of a control object. Just call the relevant setter methods of the above properties (or other methods that can modify them). However, if you want the entire app to be consistent in style, such as the style of all buttons (that is, size, background, shape, etc.) are the same, then one to repeat the style of each button, it seems too troublesome. If you can set a default appearance for the button class, it is much more convenient. Appearance proxy can be implemented.

For the navigation bar, Tabbarcontroller, and so on to manage controls for multiple view controllers, modifying their global appearance is often a convenient way to customize the default appearance, which is to use appearance proxy. The following is a detailed introduction to appearance proxy.

First, to understand a concept appearance proxy appearance agent:

Appearance agent, which is an object that manages the default appearance of visual classes, such as controls. We can modify the default appearance of this class (the default value of the appearance-related property), or the default appearance of this class in some situations, through the appearance of this class proxy.

Ii. need to comply with two agreements

For a class to be able to use an appearance proxy, there are two protocols to follow, one uiappearance and one Uiappearancecontainer protocol.

1. Uiappearance Agreement

The protocol defines 4 methods for obtaining the appearance proxy for a class, which corresponds to 4 appearance proxies respectively.

2. Uiappearancecontainer Agreement

The protocol defines a constant ui_appearance_selector that identifies the methods that the skin proxy can invoke. Since the façade proxy is used to manage the default appearance of the class, the method that can be called by it, or the method it needs to invoke, is often the setter and getter method for a certain appearance feature of the class. In other words, setting the ui_appearance_selector tag for a method means that this method can be used by the appearance agent. Of course, the format of the method is specified, the basic format and the general properties of the setter and getter method format, on this basis can add more parameters. The type of the parameter can only be Nsinteger or nsuinteger, otherwise it will be an error.

Third, the appearance of the agency's working time official on-line concise statement:

All changes to the default appearance through the appearance agent will only take effect when the view layout time is applied. After that, you can still override this default appearance through the properties and methods of the object.

My detailed explanation:

We can make the Appearance agent modify the default appearance at any time, the accurate understanding is lets the appearance agent record our custom request to the default appearance. However, the appearance agent works only once in view layout time, that is, the default appearance takes effect. After that, invoking the appearance agent to modify the default appearance does not take effect immediately, in other words, it must wait until the next view layout time for the new custom default appearance to take effect. When is view layout time? Judging from the effect of the operation, I would take it for a moment to understand that if there is a more accurate understanding of loading the window (or returning from another window to this window).

The default appearance works on all objects of the class (the so-called default, which is the meaning). If you want to modify the appearance of an object on the basis of the default appearance, just as usual, call the object's approach to the appearance settings.

Iv. acquisition and use of appearance agents

Here's a more detailed look at the 4 ways to get an appearance agent defined in the Uiappearance protocol, and its 4 appearance proxies:

1. Global appearance Agent

Features: The big Boss in the agent, which manages the default appearance of the control under any circumstances.
Method of obtaining the Proxy: + (instancetype) appearance
For example: To get the global appearance proxy for Uinavigationbar:
Uinavigation *appearanceproxy=[uinavigation appearance];

2. Appearance agent only responsible for the appearance of the specified occasion

Functionality: Manages the default appearance of a control when it is contained in a specified class (and its subclasses), and the default appearance when the management control is contained in a specified view-level relationship.
To get the Proxy method:

+ ( instancetype )appearanceWhenContainedIn:(Class<UIAppearanceContainer>)ContainerClass, …     
    • 1

The parameters of this method specify a class or classes. The number of arguments is variable, and the last parameter must be nil.
For example, get the appearance agent acting on the Uibarbuttonitem object in the Uitoolbar class, and write:

[UIBarButtonItem appearanceWhenContainedIn: [UIToolbar class],nil];
    • 1

At this point, except for the Uibarbuttonitem object that is placed in the Uitoolbar class, the Uibarbuttonitem object placed in the Uitoolbar subclass will be managed by this agent, in addition to the appearance agent management.
If you want to specify a hierarchical relationship, an object is passed in to a hierarchical relationship.

The methods described above are enabled from iOS 5.0, to iOS 9.0 discard, iOS 9.0, and later in the following ways instead:

+(instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes
    • 1

For example, set the default bartintcolor when the navigation bar is placed inside Uisplitviewcontroller or Uitabbarcontroller:

[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:  class], [UITabBarController class]]] setBarTintColor:myColor];
    • 1
3. Appearance agents that are responsible for the appearance of the control in a system environment only

Function: Manage the default appearance of the class in a certain system environment (such as horizontal screen, vertical screen, etc.). The system environment is specified by the Uitraitcollection object. (View uitraitcollection Class Reference)
To get the Proxy method:

+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait
    • 1
4. An appearance agent that is responsible for the appearance of the control in a certain system environment, some kind of situation

Features: Manages the default appearance of controls in a system environment, some kind of situation
To get the Proxy method:

+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait  whenContainedIn:(Class<UIAppearanceContainer>)ContainerClass
    • 1

The above method is enabled starting from 5.0, is discarded in iOS 9.0, iOS 9.0, and later replaced with the following method:

+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes

Customizing the default appearance of a control using appearance proxy (detailed)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.