I am customizing the UISegmentedControl, but I got a problem.
How to apply background image in the UISegmentedControl ? Changing the tint color does not fulfill my requirements.
Thanks
To change the overall color of the segmented control use its backgroundColor . To change the color of the selected segment use selectedSegmentTintColor . To change the color/font of the unselected segment titles, use setTitleTextAttributes with a state of . normal / UIControlStateNormal .
First, we need to create our variable:Three variables to customize our view with a default value. Now we need to put our buttons in view but we have an array and this can be unlimited. The best to do this is create a Horizontal Stack View. Stack View makes the subViews with equal spacing without using any constraint.
A segmented control is a linear set of two or more segments, each of which functions as a button. Within a segmented control, all segments are usually equal in width. Like buttons, segments can contain text or images. Segments can also have text labels beneath them (or beneath the control as a whole).
////Segmented Controll
NSArray *segmentTextContent = [NSArray arrayWithObjects: @"First",@"Second",@"Third",@"Forth", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.frame = CGRectMake(2, 5, 316, 35);
[segmentedControl addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.enabled = true;
segmentedControl.selectedSegmentIndex = 0;
// cutomize the font size inside segmentedControl
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:11]];
//[label setTextColor:[UIColor greenColor]];
}
}
}
You could try http://idevrecipes.com/2010/12/11/custom-segmented-controls/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With