robb/Cartography

Universal way of defining SafeArea constraints

Tylerian opened this issue ยท 5 comments

Right now we must use the #available(iOS x, *) preprocessor to switch between pre-iOS 11.0 car_topLayoutGuide / car_bottomLayoutGuide and post-iOS 11.0 safeAreaLayoutGuide.

Isn't there any cleaner way of defining those types of constraints?

Precisely !?

iPhone X in landscape mode has different guides for left, right, leading and trailing. Do you mean as making the old API available again and using it as a proxy to safe area's top and bottom automatically? I mean, I think it's good but it'd need quite a bit of refactoring as the logic is different.
When you pin to a safeAreaLayoutGuide, you match .top to .top, where as by using one of the UILayoutSupport guides you'd usually match the .top of a view to the .bottom of the guide, and vice-versa.

Yes, that's exactly what I meant. It would save us a lot of boilerplate code when defining UILayoutGuide related constraints.

I did something like this:

extension ViewProxy {
    var safeArea: SupportsPositioningLayoutProxy {
        if #available(iOS 11.0, *) {
            return safeAreaLayoutGuide
        } else {
            return self
        }
    }
}

It's not the best solution or the most elegant one, but it's better than spreading if @available through all the code.

Just an idea ๐Ÿ˜… ๐Ÿ˜‡

just opened a PR ๐Ÿ‘‰ #301