因为我的构建机器仍在使用 Xcode 12.5,所以即使我使用 @available 来检查,UITabBar 的 scrollEdgeAppearance(Xcode 12.5 的 SDK 中不存在)也会使构建失败。
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance* navBarAppearance = [UINavigationBarAppearance new];
navBarAppearance.backgroundColor = [UIColor colorNamed:@"navbar_bg"];
[UINavigationBar appearance].standardAppearance = navBarAppearance;
[UINavigationBar appearance].scrollEdgeAppearance = navBarAppearance;
UITabBarAppearance* tabBarAppearance = [UITabBarAppearance new];
tabBarAppearance.backgroundColor = [UIColor colorNamed:@"second_bg"];
[UITabBar appearance].standardAppearance = tabBarAppearance;
[UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
[UITableView appearance].sectionHeaderTopPadding = 0;
}
那么是否可以在代码中进行这种SDK检查,当构建SDK不是最新的SDK时,这些代码将不会参与构建?像这样if (BuilDSDK >= someversion)
{
[UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
}
最佳答案
@available
是一个运行时可用性检查,在这种情况下对编译时的东西并不真正有用。
在 Objective-C 中,您可以将应用在 iOS 15 SDK 上的代码部分包装到另一个宏条件中:
#ifdef __IPHONE_15_0
if (@available(iOS 15.0, *)) {
...
} else {
#endif
// possible legacy branch code
#ifdef __IPHONE_15_0
}
#endif
__IPHONE_15_0
从 iOS 15 SDK 开始定义,因此在 Xcode 12/iOS 14 SDK 中构建时被省略。 👍
关于ios - 是否可以根据 SDK 版本隐藏代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68798163/