0

Im using Auto layout with Size classes enabled.
I want to fetch data as soon as possible and it works fine doing this in:

override func viewDidLoad() {
    super.viewDidLoad()
    fetchData()
}

The problem is that the frame is not yet calculated and gives a frame of i.e 600x600 which is the canvas size in storyboard. This means that if I want to place a custom loader in the center by using:

self.loader.center = self.view.center

This will make the loader be place someplace far away from the center.
I can fix the loader placement by calling fetchData in:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    fetchData()
}

But this makes it fetch more than only one time. So my question is: When should I fetchData(), I want it to happen as early as possible. Only one time, and I want to loader to be centered.

Thanks

Edit: Disabling Auto Layout or Size classes is not an option.

user023
  • 1,450
  • 2
  • 17
  • 21
  • Simply put `self.loader.center = self.view.center` into `viewWillAppear()` and leave `fetchData()` in `viewDidLoad()` – Krumelur Jan 22 '15 at 08:21
  • hmm I mean what if fetchData would be done before viewWillAppear() ? The loader removeFromSuperview() is at the end of fetchData @krumelur – user023 Jan 22 '15 at 10:15
  • set the loader nil and check in viewWillAppear()? – Krumelur Jan 22 '15 at 12:34

2 Answers2

0

Use this, self.loader.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);

SWIFT: self.loader.center = CGPoint(x: UIScreen.mainScreen().bounds.width/2, y: UIScreen.mainScreen().bounds.height/2)

or you can try fetching your data from viewWillLayoutSubviews method

Gunjit
  • 81
  • 3
  • Fetching data in viewWilllayoutSubviews is not a good solution since that method can be called more than once. – user023 Jan 22 '15 at 12:08
0

Answering my own question:
Use:

self.view.setNeedsLayout()

to force the view to draw itself and setup the constraints.

user023
  • 1,450
  • 2
  • 17
  • 21