Define A Marco with API Check(safeAreaInsets only available in iOS 11.0 or newer)

工程中有这样的一个宏定义:

define SafeAreaInsets [UIApplication sharedApplication].delegate.window.safeAreaInsets

对它的使用引起了警告:safeAreaInsets only available in iOS 11.0 or newer

查到这篇有用的帖子:https://stackoverflow.com/questions/47590978/how-to-define-a-macro-with-api-check

于是仿写了一个方法:
UIEdgeInsets KDSafeAreaInsets() {
if (@available(iOS 11.0, *)) {
return [UIApplication sharedApplication].delegate.window.safeAreaInsets;
}
return UIEdgeInsetsZero;
}

然后把宏定义改成:#define SafeAreaInsets KDSafeAreaInsets()

然而,方案被否决了。因为KDSafeAreaInsets()这个方法写在一个新建的工具类中。项目经理对引入新的类非常敏感,他建议我再想想办法。

最后改成了这样的宏:

define SafeAreaInsets \

({UIEdgeInsets edge;
if (@available(iOS 11.0, *)) { edge = UIEdgeInsetsZero; } else { edge = UIEdgeInsetsZero; }
(edge);
})

圆满解决问题。

你可能感兴趣的:(Define A Marco with API Check(safeAreaInsets only available in iOS 11.0 or newer))