4

UIViewController's topLayoutGuide and bottomLayoutGuide are deprecated in iOS 11. What should be the replacement?

James Kuang
  • 10,710
  • 4
  • 28
  • 38

1 Answers1

14

Previously in your UIViewController:

customView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
customView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true

Now you should use:

customView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
customView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

Note the change from bottomAnchor to topAnchor. This is because the top layout guide was a rectangle at the top of the view controller, so in order to constraint your content to the top, you wanted the bottom anchor of the guide. The new safe are layout guide is a rectangle portion of the view unobscured by bars and other content, so you want the top anchor. And vice-versa for the bottom layout guide.

James Kuang
  • 10,710
  • 4
  • 28
  • 38
  • 4
    I can't seem to do this with the visual layout format language. Would you have a example for it? I'm getting '-[NSLayoutYAxisAnchor nsli_superitem]: unrecognized selector' – yuf Aug 11 '17 at 00:03
  • What the `view` does mean in `view.safeAreaLayoutGuide.topAnchor`? Is it `customView`? – chudin26 Nov 15 '21 at 21:40