Allen.L
Allen.L

Reputation: 93

iOS - Safe Area Layout not work on iOS 9 and iOS 10

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.

Upvotes: 2

Views: 4673

Answers (5)

Mehul Parmar
Mehul Parmar

Reputation: 3699

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

Upvotes: 2

Falco Winkler
Falco Winkler

Reputation: 1190

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

Upvotes: 0

mfaani
mfaani

Reputation: 36287

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…?

Upvotes: -1

Tung Fam
Tung Fam

Reputation: 8147

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.

Upvotes: 1

Rahul
Rahul

Reputation: 10625

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.

Upvotes: 0

Related Questions