0

I am using xcode 9.0.0. I am having multiple custom classes applied to different views. Whenever I run the project on a real plus size device, It shows perfect view, While when I build them on simulator, The layout squeezes from the right side. This happens with any UIView type. An an example is provided below.

enter image description here

Here, I am applying a gradient, But a part on right side is left out. And this happens with every view. My code for the above example is:

func addBlackGradientLayer(frame: CGRect){
    let gradient = CAGradientLayer()
    gradient.frame = frame
    gradient.colors = [UIColor.clear.cgColor, UIColor.black.cgColor]
    gradient.locations = [0.0, 1.0]
    self.layer.addSublayer(gradient)
}

I am totally out of any clues now. Any help will be much appreciated.

Aakash Dave
  • 866
  • 1
  • 15
  • 30
  • Are you getting warnings about conflicting or insufficient constraints? What constraints have you set? Where are you calling the code you have shown? – Paulw11 Oct 21 '17 at 21:05
  • Where are you calling this function? You are most likely calling it before the `view`’s `frame` has been fully laid out. – Paolo Oct 21 '17 at 21:05
  • @Paulw11 I tried auto-resizing/Adding missing constraints/ Setting my own constraints. Also, I am not getting any warning regarding the constraints. Even now, The image is on auto layout and no warnings/ issues. – Aakash Dave Oct 21 '17 at 21:08
  • @Paolo I am calling this is `awakeFromNib` and `viewWillAppear`. respectively for the cells and individual layout. – Aakash Dave Oct 21 '17 at 21:09
  • can it be an issue with xcode itself? – Aakash Dave Oct 21 '17 at 21:17
  • You probably need to call it from `viewDidLayoutSubviews`. Until that time, the view sizes have not been set according to the constraints, so the layer will be the wrong size – Paulw11 Oct 21 '17 at 21:27
  • @Paulw11 What you said is correct. I was calling the functions before the view sizes were loaded correctly. You saved me a huge trouble. Cheers mate! – Aakash Dave Oct 21 '17 at 21:38

1 Answers1

2

I am calling this in viewWillAppear

You must call it in viewDidAppear, it's that simple.

This is a classic "gotchya" !

PaulW11 already gave the answer, I am just putting it here to help googlers.


In viewWillAppear, obviously enough the view has not yet appeared but it will.

In viewDidAppear, the view has in fact literally appeared. That means that, absolutely, without a doubt, everything is there on the screen and of course, thus, completely laid-out.

As a rule if you're a hobbyist iOS programmer, just as a "general rule" only ever use viewDidAppear.

That is the "everyday solution" in 99% of cases.

It is fair to say viewWillAppear and viewDidLayoutSubviews and viewDidLoad are only for professional use. "Nothing bad will happen" other than the most subtle irrelevant, eg, performance effects, if you just use viewDidAppear. So, quite simply, always use viewDidAppear.

Regarding the subtle differences between viewDidLayoutSubviews, etc, it's really a big question that varies slightly from version to version and with specific arcane details of autolayout etc.; it is beyond the scope of this QA.

I'd almost say Apple shouldn't have exposed so prominently viewDidLayoutSubviews and viewWillAppear and viewDidLoad to the "general public". For example, if you're a beginner or hobbyist programmer, there is just no need at all to use things like CALayer, or to pass touches around, build your own gestures, manipulate transition animations etc.

Similarly, there is really just zero need to ever use viewDidLayoutSubviews or viewWillAppear or viewDidLoad. Just use "viewDidAppear" (in the few cases you eve need it) and you're all set.

BTW it's worth noting that if you just use container views, which you should be using everywhere at all times, (here's an epic tutorial on container views!!) these sort of UI racetrack problems rarely come up.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • `viewDidAppear` vs `viewWillAppear` vs `viewDidLayoutSubviews`, how exactly do they differ in this case? – Aakash Dave Oct 22 '17 at 06:16
  • hi, I answered your question @AakashDave. if (for some reason) you want details on that, there are a vast amount of articles, technical Apple talks etc about it – Fattie Oct 22 '17 at 15:01