In iOS 5 before custom native controls the appearance and does not support natively, so developers feel a lot of trouble. Developers are often faced with the problem is to modify all instances of a control's appearance. The correct way to solve this problem is to rewrite it controls. But because doing so is very time-consuming, some developers began to write some covered or mixed methods, such as drawRect:
.
Starting iOS 5, Apple over two protocols ( UIAppearance
and UIAppearanceContainer
) regulate many UIKit controls customized support. All follow the UIAppearance
protocols through custom UI controls can assume a variety of appearance. Moreover, UIAppearance
the agreement even allows developers to specify a different look based on regional control belongs. That is, when a control is included in a particular view, its appearance can be specified (e.g., UIBarButtonItem
a tintColor
). The appearance may be acquired proxy object class of the control, the agent with LFC achieved, the following look at an example.
All applications to customize the color of the button bar, may UIBarButtonItem
look proxy settings tintColor
:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Note, iOS 4 when the setTintColor
method in the UIBarButtonItem
middle, but it will only be applied to a particular control instance, and not all such controls. With the appearance of the proxy object, we can customize the appearance of any object using the class created.
Also, a method may be adopted according to the view contained within custom control Appearance:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor redColor]];
The first parameter is a nil
list of all the container classes end, including UINavigatorBar
, UIPopOverController
such as following the UIAppearanceContainer
protocol of the class.
IOS 5 from the start, most of the UI elements adds UIAppearance
support for the agreement. In addition, iOS 5 similar to UISwitch
the controls allows us to easily switch into designer colors on the selected color. Now, you can determine how to customize all the elements (and which attribute elements) by the appearance of a proxy UIKit under what circumstances it? There are two ways. The old way is to check the documentation, and the other way is a shortcut used by most developers: Read the header file. UIKit open header file corresponding elements, wherein all with UI_APPEARANCE_SELECTOR
attribute tags are supported by the appearance of the agent to customize. For example, UINavigationBar.h the tintColor
attribute with the UI_APPEARANCE_SELECTOR
tag:
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
It means that you can call
[[UINavigationBar appearance] setTintColor:newColor];
Although initially opposed to Apple (Mac and iOS platforms) using the UI customization, but the situation slowly changed. Apple's own native application (such as a new Reminder applications) also has the depth of customization, imitate real user interface. With the UIAppearance
agreement, the code that implements the same effect with much less.
iOS5 and later provide a more powerful tool UIAppearance, we set the global effect of some UI by UIAppearance, so that you can easily achieve custom effects of the UI but also the easiest to achieve a unified interface style, it offers the following two method.
+ (id)appearance
This method is unified all changed, for example you set tintColor UINavBar, you can write: [[UINavigationBar appearance] setTintColor: myColor];
+ (id)appearanceWhenContainedIn:(Class <>)ContainerClass,...
This method of changing a class may be provided: for example: Effect UIBarButtonItem disposed in UINavigationBar, UIPopoverController, UITabbar in. You can write
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class],[UITabbar class] nil] setTintColor:myPopoverNavBarColor];
* Please note that the use of the best UI appearance settings use the global settings, set to start before all the initialization interface, or it may fail.
DETAILED UI look modified as follows:
1. Modify the navigation bar background
code show as below:
UINavigationBar * appearance = [UINavigationBar appearance];
UIImage *navBackgroundImg =[UIImage imageNamed:@"navBg.png”];
[appearance setBackgroundImage:navBackgroundImgforBarMetrics:UIBarMetricsDefault];
2. tab bar (UITabbar)
code show as below:
UITabBar *appearance = [UITabBar appearance];
// set the background image
[appearance setBackgroundImage:[UIImage imageNamed:@"tabbar_bg.png"]];
// set the door of the selected item background image
UIImage * selectionIndicatorImage =[[UIImageimageNamed:@"tabbar_slider"]resizableImageWithCapInsets:UIEdgeInsetsMake(4, 0, 0,0)] ;
[appearance setSelectionIndicatorImage:selectionIndicatorImage];
3. A segmented control (UISegmentControl)
code show as below:
UISegmentedControl *appearance = [UISegmentedControl appearance];
// Segmenteg normal background
[appearance setBackgroundImage:[UIImage imageNamed:@"Segmente.png"]
forState: UIControlStateNormal
barMetrics:UIBarMetricsDefault];
// Segmente background check
[appearance setBackgroundImage:[UIImage imageNamed:@"Segmente_a.png"]
forState: UIControlStateSelected
barMetrics:UIBarMetricsDefault];
// Segmente dividing line at the left and right are not selected
// BarMetrics indicates the status of the navigation bar, UIBarMetricsDefault portrait represents the state (44pixel height), UIBarMetricsLandscapePhone landscape represents the state (32pixel height)
[appearance setDividerImage:[UIImage imageNamed:@"Segmente_line.png"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[appearance setDividerImage:[UIImage imageNamed:@"Segmente_line.png"]
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[appearance setDividerImage:[UIImage imageNamed:@"Segmente_line.png"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
// font
NSDictionary *textAttributes1 = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
UITextAttributeTextColor: [UIColor blueColor],
UITextAttributeTextShadowColor: [UIColor whiteColor],
UITextAttributeTextShadowOffset: [NSValuevalueWithCGSize:CGSizeMake(1, 1)]};
[appearance setTitleTextAttributes:textAttributes1 forState:1];
NSDictionary *textAttributes2 = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor blackColor],
UITextAttributeTextShadowOffset: [NSValuevalueWithCGSize:CGSizeMake(1, 1)]};
[appearance setTitleTextAttributes:textAttributes2 forState:0];
4.UIBarbutton
Note: UIBarbutton there leftBarButton, rightBarButton and backBarButton, wherein backBarButton with arrows due needs to be set individually.
barButton background setting is ios6.0 and beyond, and backbutton is ios5.0 and beyond, here to pay attention!
code show as below:
// modify UIBarButtonItem on the navigation bar
UIBarButtonItem *appearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];
// set the navigation bar fonts include backBarButton and leftBarButton, rightBarButton fonts
NSDictionary *textAttributes = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
UITextAttributeTextColor: [UIColorblueColor],
UITextAttributeTextShadowColor: [UIColorwhiteColor],
UITextAttributeTextShadowOffset: [NSValuevalueWithCGSize:CGSizeMake(1, 1)]};
[Appearance setTitleTextAttributes: textAttributes forState: 1]; // forState is 0 is a normal state, when a state of clicks.
// modify leftBarButton, rightBarButton background effects
[appearance setBackgroundImage:[UIImage imageNamed:@"navBarButton.png"]
forState: UIControlStateNormal
style:UIBarButtonItemStyleBordered
barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:[UIImage imageNamed:@"navBarButton_a.png"]
forState: UIControlStateHighlighted
style:UIBarButtonItemStyleBordered
barMetrics:UIBarMetricsDefault];
// backBarButton need to set the background effects alone. It can only be used after ios6.0
[appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"nav_bg.png"]
forState: 0
barMetrics:UIBarMetricsDefault];
[appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"work.png"]
forState: 1
barMetrics:UIBarMetricsDefault];
[appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1)
forBarMetrics: UIBarMetricsDefault];
5. Toolbar (UIToolbar)
UIToolbar *appearance = [UIToolbar appearance];
// styles and backgrounds to choose one, look at the needs of the
// style (black translucent, opaque, etc.) settings
[appearance setBarStyle:UIBarStyleBlackTranslucent];
// set the background
[appearance setBackgroundImage:[UIImage imageNamed:@"toolbarBg.png"]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
Reproduced in: https: //www.cnblogs.com/haibosoft/p/4214661.html