我有 UIViewController
和 UINavigationBarDelegate
委托(delegate),我以编程方式设置所有内容,我的应用程序是多语言的,所以我需要在 NavigationBar 等每个部分中支持 RTL(从右到左)。我使用了这种方法:
if MSGlobal.setting.language.direction == .L2R
{
navigationItem.setRightBarButtonItems(rightItems, animated: true)
}
else
{
navigationItem.setLeftBarButtonItems(rightItems, animated: true)
}
let stackCount = navigationController!.viewControllers.count
if stackCount == 1
{
let leftBtnItem:UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"drawer"), landscapeImagePhone: UIImage(named:"drawer"), style: UIBarButtonItemStyle.Plain, target: self, action: "cb_drawerTapped:")
if MSGlobal.setting.language.direction == .L2R
{
navigationItem.setLeftBarButtonItem(leftBtnItem, animated: true)
}
else
{
navigationItem.setRightBarButtonItem(leftBtnItem, animated: true)
}
}
else
{
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: self, action: "cb_back:")
}
在 LTR 语言(如英语)中一切正常,但在 RTL 语言中未显示后退按钮。我的结果一定是这样的:
LTR: [ < Title [btn1...btnn]]
RTL: [[btnn...btn1] Title > ]
最佳答案
如果您的目标是使 UIBarButtonItem
具有后退操作,您可以通过将 Semantic
更改为 Force-Right-to-Left< 通过 Storyboard 更改它
。或通过以下代码:
swift 5
在你的 ViewController
viewDidLoad()
中:
self.navigationController?.navigationBar.semanticContentAttribute = .forceRightToLeft
如果你想改变导航栏和导航 Controller 从右到左的方向,你可以像这样使用:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.semanticContentAttribute = .forceRightToLeft
}
但是你应该提到它不会改变Swipe Direction
,它只影响navigationBar,如果你想改变Swipe Direction
然后你可以使用:
self.navigationController?.view.semanticContentAttribute = .forceRightToLeft
如果您想在一行中完成所有这些并更改整个应用程序的 RTL,请使用:
UIView.appearance().semanticContentAttribute = .forceRightToLeft
关于ios - swift:使导航栏从右到左(rtl)同时带有左右项目,例如后退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33995903/