2

Here is the constraints:

enter image description here

It works fine on iOS11, but doesn't work on iOS9 and iOS 10.

enter image description here

You can download the example over here.

Note: The UISwitch must be added to the RedView and use the Safe Area Layout.

Allen.L
  • 93
  • 2
  • 12

5 Answers5

2

https://developer.apple.com/documentation/uikit/uiview/2891102-safearealayoutguide

safeAreaLayoutGuide is introduced in iOS 11. No wonder it doesn't work in iOS 9 and 10.

For older versions, consider using layoutMargins property instead.

let window = UIApplication.shared.keyWindow
let topPadding = window?.layoutMargins
Luda
  • 7,282
  • 12
  • 79
  • 139
Mehul Parmar
  • 3,599
  • 3
  • 26
  • 42
1

are you trying to achieve this? (ios10 on the left and ios 11 on the right) enter image description here

If yes, you can do next:

  1. remove safe area from red view
  2. set top constraint of UISwitch to the safe area of the view of view controller but not to the top of the red view.
Tung Fam
  • 7,899
  • 4
  • 56
  • 63
0

I checked your code and found top constraint of your Red View is not correct. Your Red View should align top from Safe Area instead of superview.

Rahul
  • 10,457
  • 4
  • 35
  • 55
0

In my case, setting edgesForExtendedLayout to an empty array for iOS 10 had the desired effect. Full answer here: https://stackoverflow.com/a/51247890/3885491

Falco Winkler
  • 1,082
  • 1
  • 18
  • 24
-1

If you're using storyboards then it would work because Storyboard is backward-compatible. Bare in mind that storyboards hides the difference in the actual code and not good for deep understanding...


If you're doing it programmatically then:

See Safe Area Layout Guide Tutorial . SafeArea is available in iOS 11 only. For non-iOS 11 you must use topLayoutGuide.bottomAnchor.

For more see On iOS, what are the differences between margins, edge insets, content insets, alignment rects, layout margins, anchors…?

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • Apple told us in [WWDC 2017 Session 412](https://developer.apple.com/videos/play/wwdc2017/412/) that Storyboards using safe areas are backwards deployable. – Allen.L Dec 26 '17 at 01:06
  • You're right. Just from a pure learning perspective, my answers points out their differences. It's better to *know* their differences and not allow Xcode just do things we don't know of. – mfaani Dec 26 '17 at 03:33
  • Okay, I will make a schedule to learn their differences, thanks for your advice. :) – Allen.L Dec 26 '17 at 04:01
  • This answer is misleading for the same reason Allen mentions. – Matt Mc Mar 04 '18 at 05:35