我有UIViewController
和UINavigationBarDelegate
委托,并且以编程方式设置了所有内容,而且我的应用是多语言的,因此在每个部分(如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
,则可以通过将Storyboard的Semantic
更改为Force-Right-to-Left
来通过Storyboard进行更改。或以下代码:
迅捷5
在你的ViewController
里面viewDidLoad()
:
self.navigationController?.navigationBar.semanticContentAttribute = .forceRightToLeft
如果要更改导航栏,并且导航控制器从右到左方向流动,则可以使用以下方式:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.semanticContentAttribute = .forceRightToLeft
self.navigationController?.view.semanticContentAttribute = .forceRightToLeft
}
如果要更改整个应用程序的RTL,请使用:
UIView.appearance().semanticContentAttribute = .forceRightToLeft
关于ios - swift :使导航栏从右到左(rtl),左右项目都像back,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33995903/