Cartography icon indicating copy to clipboard operation
Cartography copied to clipboard

Universal way of defining SafeArea constraints

Open Tylerian opened this issue 6 years ago • 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?

Tylerian avatar Sep 20 '18 12:09 Tylerian

Precisely !?

valexa avatar Sep 21 '18 08:09 valexa

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.

corujautx avatar Sep 21 '18 12:09 corujautx

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

Tylerian avatar Sep 21 '18 20:09 Tylerian

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 avatar Oct 01 '18 15:10 portellaa

just opened a PR 👉 #301

portellaa avatar Oct 01 '18 17:10 portellaa