Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TopLayoutGuide and BottomLayoutGuide Deprecated in iOS 11

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

like image 626
James Kuang Avatar asked Dec 11 '22 10:12

James Kuang


1 Answers

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.

like image 153
James Kuang Avatar answered Apr 24 '23 12:04

James Kuang