Override appearance proxy for a certain UIViewController

This content has 12 years. Please, read this page keeping its age in your mind.

OK, the case is that in your application take advantage of the appearance proxy (iOS 5) for presenting a custom UI for your app.

In AppDelegate.m you have

- (void)customizeAppearance
{
...

// UISegmentedControl
UIImage *segmentSelected = [[UIImage imageNamed:@"segcontrol_sel"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"segcontrol_uns"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"segcontrol_divider"];
UIImage *segUnselectedSelected = [UIImage imageNamed:@"segcontrol_divider"];
UIImage *segUnselectedUnselected = [UIImage imageNamed:@"segcontrol_divider"];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedUnselected forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

}

and everything is working fine until???

Until the moment that you decide that in a UiSegmentedControl in a specific UIViewController you want a different appearance.

This is the time that you want to override the universal appearance proxy behaviour and make an exception.

In order to achieve this you can use

+ (id)appearanceWhenContainedIn:(Class <UIAppearanceContainer>)ContainerClass, … NS_REQUIRES_NIL_TERMINATION;

method like:

- (void)customizeAppearance
{
   ...
// UISegmentedControl

UIImage *segmentSelected = [[UIImage imageNamed:@&quot;segcontrol_sel&quot;] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
UIImage *segmentSelectedBig = [[UIImage imageNamed:@&quot;segcontrol_sel_big&quot;] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 16, 10, 16)];

UIImage *segmentUnselected = [[UIImage imageNamed:@&quot;segcontrol_uns&quot;] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
UIImage *segmentUnselectedBig = [[UIImage imageNamed:@&quot;segcontrol_uns_big&quot;] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 16, 10, 16)];

UIImage *segmentSelectedUnselected = [UIImage imageNamed:@&quot;segcontrol_divider&quot;];
UIImage *segmentSelectedUnselectedBig = [UIImage imageNamed:@&quot;segcontrol_divider_big&quot;];

UIImage *segUnselectedSelected = [UIImage imageNamed:@&quot;segcontrol_divider&quot;];
UIImage *segUnselectedSelectedBig = [UIImage imageNamed:@&quot;segcontrol_divider_big&quot;];

UIImage *segUnselectedUnselected = [UIImage imageNamed:@&quot;segcontrol_divider&quot;];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected
                                   rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedUnselected forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:220.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@&quot;Avenir-Medium&quot; size:0.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

// override appearance proxy for PetDetailsViewController
id bigSegmentedControlAppearance = [UISegmentedControl appearanceWhenContainedIn:[PetDetailsViewController class], nil];

[bigSegmentedControlAppearance setBackgroundImage:segmentSelectedBig forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[bigSegmentedControlAppearance setBackgroundImage:segmentUnselectedBig forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[bigSegmentedControlAppearance setDividerImage:segmentSelectedUnselectedBig forLeftSegmentState:UIControlStateSelected
                                 rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[bigSegmentedControlAppearance setDividerImage:segUnselectedSelectedBig forLeftSegmentState:UIControlStateNormal
                                 rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

}

At the beginning we define two versions for each image and then using appearanceWhenContainedIn method we define the appearence for of UISegmentedControl only for the specific UIViewController.

Credits to Mr.Dave DeLong

2 thoughts on “Override appearance proxy for a certain UIViewController

  1. Good way of describing, and fastidious paragraph to obtain facts regarding my
    presentation focus, which i am going to convey in academy.

Comments are closed.