Note that there are some explanatory texts on larger screens.

plurals
  1. POHeight issue with custom UISegmentedControl following rotation to landscape
    primarykey
    data
    text
    <h3>PROBLEM</h3> <p>I have created a custom <code>UISegmentedControl</code> component for an iOS application, placed on the navigation bar, with two segments. As one would expect, the component has a slightly shorter height when the phone is rotated to landscape orientation.</p> <p>However, the problem is that after rotating to landscape, the segmented control retains its short height even after rotating back to portrait. I appreciate your help.</p> <p>Here's the (simplified) code, which is inside the <code>initWithCoder:</code> method:</p> <pre><code>UIImage *bg = [[UIImage imageNamed:@"button-bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(7, 7, 7, 7)]; UIImage *bgH = [[UIImage imageNamed:@"button-bg-h"] resizableImageWithCapInsets:UIEdgeInsetsMake(7, 7, 7, 7)]; [self setBackgroundImage:bg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [self setBackgroundImage:bg forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone]; [self setBackgroundImage:bgH forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; [self setBackgroundImage:bgH forState:UIControlStateSelected barMetrics:UIBarMetricsLandscapePhone]; </code></pre> <p>============================</p> <h3>TEMPORARY WORKAROUND</h3> <p>Here's my temporary workaround for it, until I find a better solution:</p> <p>In the view controller, I have an outlet called <code>segmentControl</code>, linked to the <code>UISegmentedControl</code> in question. I also created a property as follows:</p> <pre><code>@property CGRect segmentedControlPortraitOrientationFrame; </code></pre> <p>Inside the <code>viewDidLoad</code> method:</p> <pre><code>self.segmentedControlPortraitOrientationFrame = self.segmentControl.frame; </code></pre> <p>Then override <code>didRotateFromInterfaceOrientation:</code> with the following implementation:</p> <pre><code>-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { [super didRotateFromInterfaceOrientation:interfaceOrientation]; if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) self.segmentControl.frame = self.segmentedControlPortraitOrientationFrame; } </code></pre> <p>The workaround assumes that apps always load in portrait orientation first, which I think is true, as long as the app supports portrait. Correct me if I'm wrong.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload