According to XCode, “topLayoutGuide” is depricated as of iOS 11.0. Developer documentation says to use safeAreaLayoutGuide instead.
I experienced the same with IOS 11.0 and additionally the margin.leadingAnchor
does not work.
So I replaced
let leadingConstraint =
segmentedControl.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
with
let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20)
and -20 for the trailingAnchor
@Orangecicle I was able to implement safeAreaLayoutGuide using the following code:
// the top anchor of the segmented control should be 8 points below the top of the view’s safeAreaLayoutGuide
let topConstraint = segmentedControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8)
@katiekalt I had the problem with the topLayoutGuide not working with iOS 11.0, but I didn’t have any problems with the margins.leadingAnchor. Typo perhaps? Is my first line in your code as well?
Here’s my code, same as in the book:
let margins = view.layoutMarginsGuide
let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
let trailingConstraint = segmentedControl.trailingAnchor.constraint(equalTo: margins.trailingAnchor)
Using the safeAreaLayoutGuide worked good for me (as long as translatesAutoresizingMaskIntoConstraints is set to false)
let safeArea = view.safeAreaLayoutGuide
let topConstraint = segmentedControl.topAnchor.constraint(equalTo: safeArea.topAnchor)
let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor)
let trailingConstraint = segmentedControl.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor)
topConstraint.isActive = true
leadingConstraint.isActive = true
trailingConstraint.isActive = true
With layoutMarginsGuide:
let margins = view.layoutMarginsGuide
let topConstraint = segmentedControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
let trailingConstraint = segmentedControl.trailingAnchor.constraint(equalTo: margins.trailingAnchor)