robb / Cartography

A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:
Other
7.35k stars 524 forks source link

Universal way of defining SafeArea constraints #300

Open Tylerian opened 5 years ago

Tylerian commented 5 years ago

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?

valexa commented 5 years ago

Precisely !?

corujautx commented 5 years ago

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.

Tylerian commented 5 years ago

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

portellaa commented 5 years ago

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 😅 😇

portellaa commented 5 years ago

just opened a PR 👉 #301