我是Swift的新手,当用户单击阿拉伯语语言按钮时,我正在尝试将应用程序视图方向从LTR更改为RTL。 到目前为止,我在下面有这个代码,所有文本都正确更改,除非方向。只有在我关闭并重新打开应用程序时,方向才有效。 请帮忙
@IBAction func goToLanguage(sender: AnyObject) {
let availableLanguages = Localize.availableLanguages()
actionSheet = UIAlertController(title: nil, message: "Switch Language", preferredStyle: UIAlertControllerStyle.ActionSheet)
for language in availableLanguages {
let displayName = Localize.displayNameForLanguage(language)
let langString = language as String
if(langString != "Base"){
let languageAction = UIAlertAction(title: displayName, style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
Localize.setCurrentLanguage(language)
NSUserDefaults.standardUserDefaults().setObject([language], forKey: "AppleLanguages")
NSUserDefaults.standardUserDefaults().synchronize()
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
delegate.refreshEverything()
})
actionSheet.addAction(languageAction)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(alert: UIAlertAction) -> Void in
})
actionSheet.addAction(cancelAction)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
答案 0 :(得分:5)
您可以在不关闭应用的情况下强制使用UIView方向。 对于阿拉伯语言,请添加以下代码以更改方向:
UIView.appearance().semanticContentAttribute = .forceRightToLeft
并使用法语或英语:
UIView.appearance().semanticContentAttribute = .forceLeftToRight