Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the height of UISegmentedControl in iOS

Problem: How to change the height of the UISegmentedControl in iOS?

After Searching, I got the following answers:

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGRect frame= segmentedControl.frame;
    [segmentedControl setFrame:CGRectMake(10.0, 20.0, 200.0, 50.0)];
}

But this resets the height of the control to default if I select any option of the segmented control in iOS 7

like image 216
Simranjeet Kaur Avatar asked Dec 02 '22 19:12

Simranjeet Kaur


1 Answers

If you have autolayout, set the constraints of the UISegmentedControl. Please see pic of where's easiest to set this, make sure width and height are ticked, plus vertical and horizontal space constraints (select where lines that are deep orange in pic).

enter image description here

Now that you have this, control-drag the constraint that sets the height of the segmented control to your header file and name it something like segmentedControlHeightConstraint.

Once you have done that, within your viewDidLoad in your view controller implementation file add this code

self.segmentedControlHeightConstraint.constant = 50; // or whatever height you wish

This is the best way using auto layout to set the height for this.

Hope this helps

like image 69
Jim Tierney Avatar answered Dec 21 '22 22:12

Jim Tierney