Is this possible to support iPhone X and iOS 8 in same project?

Viewed 1140

It seems that a "Safe area layout guide before ios 9". error will occur if I trigger the Use Safe Area Layout Guides in the least XCode, does it means supporting both devices is not possible? Any advice? Thanks.

1 Answers

It is totally possible to support iPhone X with a minimum target of iOS 8. (In fact, that's what we currently have in the Khan Academy app.)

What we do is apply the safeAreaInsets in our Swift code using the #available function, like so:

public override func safeAreaInsetsDidChange() {
    if #available(iOS 11.0, *) {
        super.safeAreaInsetsDidChange()
        self.contentCatalogHeaderView?.safeAreaInsetsTopOverride = safeAreaInsets.top
        self.collectionViewLayout.safeAreaInsetsTop = safeAreaInsets.top
    }
}

From your question, it sounds like you're debating whether to use the checkbox in a Storyboard to enable safe area insets. I'm not sure whether it's possible for a Storyboard to support iOS 8 if safe area insets are enabled (I suspect it's not possible). However, you can always store a reference to a layout constraint, and update its constant in your code using the above #available function.

(In Objective-C, the code looks nearly identical, just format it like this:

- (void)viewSafeAreaInsetsDidChange {
    if (@available(iOS 11.0, *)) {
        [super viewSafeAreaInsetsDidChange];
        [self.view setNeedsLayout];
    }
}
Related