Ios 基于系统语言更改用户界面-Swift
我有一个应用程序,我想支持两种语言。英语和希腊语。 我已经本地化了整个应用程序,除了用户界面,其他一切都很完美。 我想根据用户的系统语言更改图像。它在模拟器上运行良好,但当我使用我的真实设备时,应用程序会改变语言,除了图像 这是我的更改代码Ios 基于系统语言更改用户界面-Swift,ios,swift,xcode,localization,Ios,Swift,Xcode,Localization,我有一个应用程序,我想支持两种语言。英语和希腊语。 我已经本地化了整个应用程序,除了用户界面,其他一切都很完美。 我想根据用户的系统语言更改图像。它在模拟器上运行良好,但当我使用我的真实设备时,应用程序会改变语言,除了图像 这是我的更改代码 let language = NSLocale.current.identifier override func viewDidLoad() { super.viewDidLoad() setupUI() } func s
let language = NSLocale.current.identifier
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
if language == "el_US" {
print(language)
scanBtn.setBackgroundImage(UIImage(named: "scanBtnGr"), for: .normal)
detoxBtn.setBackgroundImage(UIImage(named: "detoxGr"), for: .normal)
bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
searchBtn.setBackgroundImage(UIImage(named: "browseGr"), for: .normal)
captureBtn.setBackgroundImage(UIImage(named: "scanGr"), for: .normal)
uploadBtn.setBackgroundImage(UIImage(named: "uploadGr"), for: .normal)
}else{
print(language)
scanBtn.setBackgroundImage(UIImage(named: "scanBtn"), for: .normal)
detoxBtn.setBackgroundImage(UIImage(named: "detoxBtn"), for: .normal)
bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
searchBtn.setBackgroundImage(UIImage(named: "browseBtn"), for: .normal)
captureBtn.setBackgroundImage(UIImage(named: "scan"), for: .normal)
uploadBtn.setBackgroundImage(UIImage(named: "upload"), for: .normal)
}
}
我真的不知道问题出在哪里,因为在模拟器上工作得很好。改变设置这些图像的方式,如下所示可能会有所帮助
scanBtn.setBackgroundImage(UIImage(named: NSLocalizedString(key: "scanBtnGr", comment: ""), for: .normal)
这样,您就不必担心目标语言代码。您可以删除if语句并在Localizable.strings
文件中定义相应的名称
此外,如果在不终止应用程序和更改设备语言的情况下更改应用程序的语言,请确保在语言切换后再次调用
setupUI()
函数<代码>视图将出现可能是它的合适位置 谢谢你的回答。我将如何在Localizable.strings文件中定义图像。另外,我必须提到这些图像在assets文件夹中。将它们放在assets文件夹中是可以的。例如,您需要有一行,例如“scanBtn”=“scanBtnGr”代码>在Localizable.strings(希腊文)
中。在此之前,您需要确保正确设置本地化。您可以通过此链接了解有关本地化的更多信息。它工作得很好。非常感谢。另外,我没有将setupUI
放在视图中,视图将显示。它在viewDidLoad
中运行良好。是否尝试调试它?你使用断点吗?您的语言可能与el_US不同