Changing appearance right after changing app language on fly causing problem in iOS using swift

200 Views Asked by At

I have both options Appearance and change Language (to Arabic) in my iOS app. When you change language and try to change appearance, some UI elements restores their direction i.e RTL to LTR

I'm doing method swizzling for changing app language, Don't want to restart the app. I think this is causing the problem.

Here is how i am changing ui appearance

UIApplication.shared.windows.first?.overrideUserInterfaceStyle = theme.uiInterfaceStyle

Note: If you restart the app after changing language, and change appearance work fine.

3

There are 3 best solutions below

0
Ahsan Ebrahim Khatri On BEST ANSWER

I had a similar problem. Since you are messing up with the UIWindow, right after overriding appearance, it restores your previous method swizzling.

What you need to do is to call the change language method again to the currently selected/set language.

0
Chandaboy On

check this link

https://betterprogramming.pub/ios-13-is-your-app-ready-for-the-dark-6aa73adec14b

let myDynamicColor = UIColor { (traitCollection: UITraitCollection) -> UIColor in 
    if traitCollection.userInterfaceStyle == .dark {
        return UIColor(red: 0, green: 0, blue: 0, alpha: 1.0)
    } else {
        return UIColor(red: 220, green: 220, blue: 220, alpha: 1.0)
    }
}
0
Chaman Sharma On

You can use NSNotificationCenter and addObserver in required screens. Just send notification to all screens when appearance changed and refresh screen by addObserver method:

- (void)postNotificationDataChanged{
    [[NSNotificationCenter defaultCenter] postNotificationName:AppearanceChanged object:nil userInfo:nil];
}


[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:AppearanceChanged object:nil];