Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the UISegmentedControl in iOS?

Tags:

ios

iphone

ipad

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

like image 842
user403015 Avatar asked Aug 17 '11 08:08

user403015


People also ask

How do you change the segment control color?

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 .

How do I create a custom segmented control in Swift?

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.

What is segmented control?

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).


2 Answers

////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]];
        }
    }           
}
like image 99
Nur-E-Alam Khan Avatar answered Sep 20 '22 11:09

Nur-E-Alam Khan


You could try http://idevrecipes.com/2010/12/11/custom-segmented-controls/.

like image 37
Maximilian Liesegang Avatar answered Sep 22 '22 11:09

Maximilian Liesegang