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:@"segcontrol_sel"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)]; UIImage *segmentSelectedBig = [[UIImage imageNamed:@"segcontrol_sel_big"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 16, 10, 16)]; UIImage *segmentUnselected = [[UIImage imageNamed:@"segcontrol_uns"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)]; UIImage *segmentUnselectedBig = [[UIImage imageNamed:@"segcontrol_uns_big"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 16, 10, 16)]; UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"segcontrol_divider"]; UIImage *segmentSelectedUnselectedBig = [UIImage imageNamed:@"segcontrol_divider_big"]; UIImage *segUnselectedSelected = [UIImage imageNamed:@"segcontrol_divider"]; UIImage *segUnselectedSelectedBig = [UIImage imageNamed:@"segcontrol_divider_big"]; 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]; [[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:@"Avenir-Medium" 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
Good way of describing, and fastidious paragraph to obtain facts regarding my
presentation focus, which i am going to convey in academy.
Thanks for sharing your thoughts on reviews. Regards