A segmented control is a tool which provides segments as buttons. Each segment can be programmed to perform an action. Usually it is required to customize or modify the appearance of the UISegmentedControl in order to make it look like the rest of the app or to blend in with the rest of the components of the screen.
Customizing a segmented control is a bit tricky because the segments are connected with each other and share borders with their neighboring segments. Therefore we need images, to change the segments as well as the borders of their neighboring segments.
Here's the piece of code that will make this task a lot simpler and easier.
+(void)CustomizeSegmentControl
{
UIImage *segmentSelected = [UIImage imageNamed:@"segment_selected.png"]resizableImageWithCapInsets:UIEdgeInsetMake(0,7,0,5)];
UIImage *segmentUnselected = [UIImage ImageNamed:@"segmented_unselected.png"]resizableImageWithCapInsets:UIEdgeInsetMake(0,7,0,5)];
UIImage *SegmentSelectedUnselected = [UIImage imageNamed:@"segment_selected_unselected.png"];
UIImage *segmentUnselectedSelected = [UIImage imageNamed:@"segment_unselected_selected.png"];
UIImage *segmentUnselectedUnselected = [UIImage imageNamed:@"segment_unselected_unselected.png"];
[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetricsDefault];
[UISegmentedControl appearance] setBackgroundImage:segmentSelected forState:UIControlStateNormal barMetricsDefault];
[UISegmentedControl appearance] setDividerImage: segmentUnselectedUnselected forLeftSegmentState:UIControlStatementNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[UISegmentedControl appearance] setDividerImage: segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected rightSegmentState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
[UISegmentedControl appearance] setDividerImage: segmentSelectedUnselectedSelected forLeftSegmentState:UIControlStateNormal rightSegmentState: UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont boldSystemOfSize:12.0], UITextAttributeFont, [UIColor blackColor],UITextAttributeTextColor,[NSValue valueWithUIOffset:UIOffsetMake(0,1)], UITextAttributeTextShadowColor,nil] forState:UIControlStateNormal];
}
0 Comment(s)