Size class initially unknown

286 views Asked by At

I am trying to setup my UI on my universal app. I have a storyboard setup with size classes, a fairly simple UI. I have my view controller with a view in. Inside this view, I draw a chart so this can only be updated using setFrame.

This is where my problem begins. I set my graph to be the screen width. However, when the view initially runs, the size class seems to be unknown.

As the default 'Any' size in my storyboard is 600x600. My view thinks it should draw 600 wide on my iPhone, which clearly isn't this wide.

It is only after I physically move the iPhone to toggle an orientation change, that it updates and recognises the correct size.

So my question is, how do I prevent this problem? I need my UI to know what size to be from the get go, not just after the user rotates their iPhone.

2

There are 2 answers

4
Bimal Appvolution On BEST ANSWER

There are two to prevent this problem

(1) Load your entire method in

-(void)viewDidAppear:(BOOL)animated
{
}

(2) Do the following step

  • Go to file Inspector
  • Uncheck "Use size classed
0
matt On

However, when the view initially runs, the size class seems to be unknown.

It is unknown to the view and the view controller, because at that time the view controller is not yet part of the interface and has no environment. But it is not unknown to UIScreen.mainScreen(). So if you need this information very early, that is who to ask.

However, as you've been advised in a comment, it also sounds like you may simply be doing this too early. Nothing in a view controller's view, including the view itself, has achieved its actual size until viewDidLoad or later.