2

I'm having an issue when trying to layout a view programatically and I cant seem to find a concise, non hacky way to fix it.

I'm using safeAreaInsets to size some elements in my view. This works well until I try it out on a pre iOS 11 device. Obviously, with the lack of safeAreaInsets the sizing of my subviews falls apart and everything becomes a mess. What do I fall back to when using older versions of iOS.

More specifically, what can I implement in the extension below that will work as expected?

extension UIView {

func compatibilityInsets() -> UIEdgeInsets {
        if #available(iOS 11.0, *) {
            return self.safeAreaInsets
        } else {
            //what goes here?
            return self.olderVersionOfInsets
        }
    }
}

Here is an exmaple of how I might use this extension method:

var minimumHeaderHeight: CGFloat {
    //allows the header height to be 70 below navigation bar
    return 70 + view.compatibilityInsets().top
}
Keyur Tailor
  • 424
  • 3
  • 16
Craigt
  • 3,418
  • 6
  • 40
  • 56

1 Answers1

2

safeAreaInsets was added to help avoid content disappearing behind the "notch" in the iPhone X... which only supports iOS 11 IIRC.

So the alternative for iOS 10 and below would be return .zero as there doesn't need to be any safe area defined.

.zero in this case is inferred to be of type UIEdgeInsets so is equivalent to calling UIEdgeInsets.zero.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306