Since topLayoutGuide
property is now deprecated in iOS 11, what is alternative to use top layout guide?
backView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor)
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).
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'.
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.
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.
TopLayoutGuide is deprecated in iOS 11 so we have option to use SafeAreaLayoutGuide like this :
let guide = view.safeAreaLayoutGuide
Second add constraint to guide
searchBackView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With