swift:使导航栏从右到左(rtl)包含左右

时间:2015-11-30 10:04:23

标签: ios swift localization internationalization right-to-left

我有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      > ]

1 个答案:

答案 0 :(得分:2)

如果您的目标是使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 = .forceLeftToRight