Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top layout guide is deprecated in iOS 11

Since topLayoutGuide property is now deprecated in iOS 11, what is alternative to use top layout guide?

backView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor)
like image 527
Sukhpreet Avatar asked Aug 19 '17 02:08

Sukhpreet


People also ask

What is top layout guide?

The topLayoutGuide property comes into play when a view controller is frontmost onscreen. It indicates the highest vertical extent for content that you don't want to appear behind a translucent or transparent UIKit bar (such as a status or navigation bar).

How do you add top and bottom layout in storyboard?

Select your view. Select 'Add New Constraints' option in the InterfaceBuilder. From the dropdown menu of top or bottom constraint, select the 'Top Layout Guide' or 'Bottom Layout Guide'.

What is layout guide iOS?

The safe area layout guide is a property of the UIView class and it inherits from the UILayoutGuide class. It was introduced in iOS 11 and tvOS 11. It helps you correctly position views in a layout. The safe area layout guide replaces the top and bottom layout guides of the UIViewController class.

What is Autolayout in iOS Swift?

Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.


2 Answers

TopLayoutGuide is deprecated in iOS 11 so we have option to use SafeAreaLayoutGuide like this :

  • First we can get view safeAreaLayoutGuide
 let guide = view.safeAreaLayoutGuide
  • Second add constraint to guide

    searchBackView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
    
like image 183
Sukhpreet Avatar answered Oct 24 '22 03:10

Sukhpreet


The Obj-C version:

[[searchBackView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:0] setActive:YES];

If you have translucent bars note: then the topLayoutGuide is below the bar, e.g the statusBar.

see: topLayoutGuide It explains conditions which is important to consider.

Whereas the safeAreaLayoutGuide.topAnchor would not be beneath the NavigationBars; much simpler to work with.

like image 39
Wayne Avatar answered Oct 24 '22 03:10

Wayne