traitCollection.preferredContentSizeCategory.isAccessibilityCategory for iOS 10

Viewed 1025

The following code works great on iOS11 to detect if the user has set LARGE FONT in their accessibility settings.

However, I need to support this in iOS10 as well. How can I accomplish this?

Right now the code looks like this:

if #available(iOS 11.0, *) {
    if traitCollection.preferredContentSizeCategory.isAccessibilityCategory {
        return UITableViewAutomaticDimension
    } else {
        return someSpecificHeight
    }
} else {
    // how to detect is isAccessibilityCategory on non-iOS11 devices?
    // is there some ugly fallback I don't know about?
}
3 Answers

Okay, based on @Jefflovejapan's answer, it looks like I can do this:

 let sizeCategory = traitCollection.preferredContentSizeCategory

        if sizeCategory == .accessibilityMedium
     || sizeCategory == .accessibilityLarge
     || sizeCategory == .accessibilityExtraLarge
     || sizeCategory == .accessibilityExtraExtraLarge
     || sizeCategory == .accessibilityExtraExtraExtraLarge {
            return UITableViewAutomaticDimension
        } else {
            return someSpecificHeight
        }

Ugly, but I think it does the trick..

I have to do all the == comparisons, because that seems like the only supported operator in iOS10 (all the other ones apparently are added in iOS11)

Expanding on Jeff's answer. I found it cleaner to add an extension to UITraitCollection. Hope this helps for any iOS dev still having to develop for iOS 10 (sad times).

extension UITraitCollection {
    var isAccessibleCategory: Bool {
        if #available(iOS 11, *) {
            return preferredContentSizeCategory.isAccessibilityCategory
        } else {
            switch preferredContentSizeCategory {
            case .accessibilityExtraExtraExtraLarge,
                 .accessibilityExtraExtraLarge,
                 .accessibilityExtraLarge,
                 .accessibilityLarge,
                 .accessibilityMedium:
                return true
            default:
                return false
            }
        }
    }
}

Can then be used like this

return traitCollection.isAccessibleCategory ? UITableView.automaticDimension : someSpecificHeight

It looks like .accessibilityMedium is the next size up from .extraExtraExtraLarge, so maybe that could be your threshold.

Related