Programmatically move a segmented control.

I have a .xib file that I use for options. One of the options is a sound delay before I play a synthesized sound. Since synthesized sound is not available before iOS7, I figured that I’d hide the option. But that leaves a gap in the screen where the control used to be. This code moves the next segmented control up to where the original one was. Make sure you link the text label to the Files Owner by control clicking and dragging from the Files Owner to the label in Interface Builder section of Xcode.


if ( SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"7.0") ) {

    // Hide the delay control
    self.chooseDelaySegmentedControl.hidden = YES;
    self.targetSoundDelayText.text = @"";

    // Move the segmented control up
    CGRect scoringTypeFrame = self.chooseScoringTypeSegmentedControl.frame;
    scoringTypeFrame.origin.y = scoringTypeFrame.origin.y - 70.0f;
    self.chooseScoringTypeSegmentedControl.frame = scoringTypeFrame;
    
    CGRect scoringTypeTextFrame = self.scoringChoiceText.frame;
    scoringTypeTextFrame.origin.y = scoringTypeTextFrame.origin.y - 70.0f;
    self.scoringChoiceText.frame = scoringTypeTextFrame;
    
} 

Oddly enough, you can’t just change the x or y value, you need to change the whole frame.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.