0

I have a custom view that I'm adding to a view controller in viewDidLoad() -

var customView : CustomView = CustomView()

override func viewDidLoad(){
  super.viewDidLoad()
  self.view.addSubView(customView)
  customView.topAnchor.constraint(equalTo : view.safeAreaLayoutGuide.topAnchor).isActive = true //Tried this but nothing happens
 }

I have a strange 20px padding on top of the view. I want the custom subview to begin from under the navigation bar. I've tried multiple things but I'm unable to get the 20px top padding to go away. How do I constraint the top of my view to under the navigation bar at all times?

entropy.maximum
  • 477
  • 4
  • 16

1 Answers1

0

Don't rely on the safeAreaLayoutGuide in the viewDidLoad method - since the view hasn't laid out yet in there. If you do it (let's say) in viewDidLayoutSubviews, you should get the valid value.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84