在iOS中,使用Swift在动态更改应用程序语言后立即更改外观会导致问题。

huangapple go评论68阅读模式
英文:

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

问题

我在我的iOS应用中有两个选项,外观和更改语言(为阿拉伯语)。当您更改语言并尝试更改外观时,一些UI元素会恢复它们的方向,即从RTL到LTR。

我正在使用方法交换来更改应用程序语言,不想重新启动应用程序。我认为这是问题的原因。

以下是我如何更改UI外观的方式:

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

注意:如果在更改语言后重新启动应用程序,更改外观可以正常工作。

英文:

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.

答案1

得分: 1

请查看以下链接:

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)
    }
}
英文:

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)
    }
}

答案2

得分: 1

我遇到了类似的问题。由于你正在操作UIWindow,所以在覆盖外观之后,它会恢复先前的方法交换。

你需要做的是再次调用更改语言的方法以使用当前选择/设置的语言。

英文:

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.

答案3

得分: 0

你可以在需要的屏幕上使用NSNotificationCenter和addObserver。当外观发生变化时,只需向所有屏幕发送通知,并使用addObserver方法刷新屏幕:

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

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

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];

huangapple
  • 本文由 发表于 2023年7月3日 15:25:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定