iOS 让界面元素的文字随着语言的更改而变化——本地化文字跟随

在我的 App 内置的设置中,修改了语言,这时需要让当前界面的文本跟着改变语言。

解决方法是:添加一个观察者,观察 localize 本地语言的通知,然后一有变化就调用自定义的方法执行操作。(而设置中其实是改变了 localize 的语言)

swift 复制代码
override func viewDidLoad() {
        tableForChoose = tableChoose
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
}
@objc func setText() {
        btn_complete?.setTitle("done".localized(), for: .normal)
        tableForChoose?.reloadData()
        tableForChoose?.selectRow(at: defaultChoose ?? IndexPath(row: 0, section: 0), animated: true, scrollPosition: .middle)
        if let cell = tableForChoose?.cellForRow(at: defaultChoose ?? IndexPath(row: 0, section: 0)) {
            cell.accessoryType = .checkmark
        }
}
 
相关推荐
HarderCoder16 小时前
Swift 6 并发时代,如何优雅地“抢救”你的单例?
swift
zhangmeng16 小时前
FlutterBoost在iOS26真机运行崩溃问题
flutter·app·swift
HarderCoder16 小时前
SwiftUI 踩坑记:onAppear / task 不回调?90% 撞上了“空壳视图”!
swift
HarderCoder17 小时前
@isolated(any) 深度解析:Swift 并发中的“隔离追踪器”
swift
大熊猫侯佩20 小时前
桃花岛 Xcode 构建秘籍:Swift 中的 “Feature Flags” 心法
app·xcode·swift
用户0921 小时前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan21 小时前
iOS26适配指南之UIColor
ios·swift
HarderCoder1 天前
Swift 6.2 新特性 `@concurrent` 完全导读
swift
HarderCoder1 天前
Swift 里的“橡皮擦”与“标签”——搞懂 existentials 与 primary associated type
swift
权咚2 天前
阿权的开发经验小集
git·ios·xcode