I have a shared library (for mac and iOS app), and I'd like to use UIContentSizeCategory there. Since it is a part of UIKit, I added
import SwiftUI
#if canImport(UIKit)
import UIKit
#endif
to a file header and then declare my extension (to convert from SwiftUI to UIKit size category):
#if canImport(UIKit)
@available(iOS 8.0, macCatalyst 13.1, tvOS 9.0, *)
extension ContentSizeCategory {
var uiContentSizeCategory: UIContentSizeCategory {
switch self {
case .extraSmall: return .extraSmall
case .small: return .small
case .medium: return .medium
case .large: return .large
case .extraLarge: return .extraLarge
case .extraExtraLarge: return .extraExtraLarge
case .extraExtraExtraLarge: return .extraExtraExtraLarge
case .accessibilityMedium: return .accessibilityMedium
case .accessibilityLarge: return .accessibilityLarge
case .accessibilityExtraLarge: return .accessibilityExtraLarge
case .accessibilityExtraExtraLarge: return .accessibilityExtraExtraLarge
case .accessibilityExtraExtraExtraLarge: return .accessibilityExtraExtraExtraLarge
@unknown default:
assertionFailure("Unknown ContentSizeCategory: \(self)")
return .medium
}
}
}
#endif
However, I still get Cannot find type 'UIContentSizeCategory' in scope error when building this, even though I have checked platform availability of UIContentSizeCategory in docs and added @available check for these platforms.
How should I properly limit the availability of the extension to make it build?
Thanks for any advice! Tomas
EDIT: Very weird thing is that if I change the check to #if !os(watchOS) && canImport(UIKit), it builds.
Why is that? Isn't @available(iOS 8.0, macCatalyst 13.1, tvOS 9.0, *) enough to exclude watchOS target?