0

I want display different UIButton/UILabel/UISlider sizes on an iPhone 4/4s screen. In the parent ViewController viewDidAppear: method I was able to set new button frames by calling my viewFormatting function.

- (void) viewFormatting
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;

        //iPhone 4
        if (result.height == 480)
        {
            self.myButton.frame = CGRectMake(x, y, width, height);
            self.myLabel.frame = CGRectMake(x, y, width, height);
            //etc.
        }
    }
}

But when I try calling the same method viewFormatting inside initWithFrame: on separate UIView class the button/label frames do not resize. I also tried calling the function inside layoutSubviews. I put a NSLog inside viewFormatting so I know it is getting called correctly, but the button/label frames do not change.

Note: In both scenarios I have created the outlets in XIB files.

Any help is greatly appreciated!

Seslyn
  • 807
  • 10
  • 19
  • Write it in ViewDidLayoutSubview – Jageen Dec 15 '14 at 17:41
  • Is this the case ? http://stackoverflow.com/questions/19515924/redraw-view-off-screen-contains-picker-view-and-tool-bar/19532803#19532803 – Jageen Dec 15 '14 at 17:42
  • are you using **AutoLayout**? – nburk Dec 15 '14 at 17:43
  • Not using AutoLayout, do I need to call viewDidLayoutSubview? Or is it automatically called like viewDidAppear? – Seslyn Dec 15 '14 at 17:44
  • 1
    @nburk Ah, I was using AutoLayout. I unchecked that and it works fine. Didn't realize that individual xibs could use auto layout individually. Thanks! – Seslyn Dec 15 '14 at 17:49
  • @Seslyn, viewDidLayoutSubview will called automatically like viewDidAppear , and if you are developing new application use storyboard and size classes – Jageen Dec 15 '14 at 17:51
  • @Jageen thanks for your help! I figured out my issue was with AutoLayout. – Seslyn Dec 15 '14 at 17:53

2 Answers2

0

Update: Thanks to @nburk I realized that the use AutoLayout was checked on my xib file. After removing that the code above works perfectly.

I'll leave it here for anyone who needs a sample.

Seslyn
  • 807
  • 10
  • 19
0

Add this method: -(void) viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self viewFormatting]; }

viewDidLayoutSubviews get called automatically.