Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

ios - Overriding `preferredContentSizeCategory` requires an app restart to take effect

In my iOS app, I have overridden preferredContentSizeCategory on my UIApplication subclass to return a custom setting value:

class MyApplication: UIApplication {
    override var preferredContentSizeCategory: UIContentSizeCategory {
        // GeneralSettings.textSizeOverride is stored in UserDefaults
        GeneralSettings.textSizeOverride ?? super.preferredContentSizeCategory
    }
}

I want the text size to be configurable from within the app, allowing a user to override the system default text size and set a value specific for this app. The selected value in stored in UserDefaults.

I can't get the app's UI to refresh taking account of the new preferred size, unless I force quite and relaunch the app. Even if I re-instantiate the stack of view controllers, I see the text sizes based on the value that preferredContentSizeCategory had upon launch.

When the user changes the app's text size setting, I am posting a notification to try to get the UI to update:

NotificationCenter.default.post(
    name: UIContentSizeCategory.didChangeNotification, 
    object: UIScreen.main, 
    userInfo: [
        UIContentSizeCategory.newValueUserInfoKey: GeneralSettings.textSizeOverride!.rawValue
    ]
)

It seems like something in the application has cached the preferredContentSizeCategory value at launch, and won't use the UIApplication-defined value until next launch. Is there a way to achieve this?

question from:https://stackoverflow.com/questions/65881545/overriding-preferredcontentsizecategory-requires-an-app-restart-to-take-effect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...