简体   繁体   English

swift:使导航栏从右到左 (rtl) 使用左右项目,例如 back

[英]swift: make navigation bar Right-to-left (rtl) with both right and left items like back

I have UIViewController with UINavigationBarDelegate delegate and I set everything's programmatically and my app is multi language so I need RTL (Right to Left) support in every section like NavigationBar.我有带有UINavigationBarDelegate委托的UIViewController ,我以编程方式设置了所有内容,我的应用程序是多语言的,所以我需要在每个部分(如 NavigationBar)中支持 RTL(从右到左)。 I used this approach:我使用了这种方法:

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:")
}

Everything's work fine in LTR language like English, But in RTL language back button not shown.在 LTR 语言(如英语)中一切正常,但在 RTL 语言中未显示后退按钮。 My result must be like this:我的结果一定是这样的:

LTR:   [ <      Title      [btn1...btnn]]
RTL:   [[btnn...btn1]      Title      > ]

If your goal is to have right UIBarButtonItem with back action , you can change it through Storyboard by changing Semantic to Force-Right-to-Left .如果您的目标是拥有正确的UIBarButtonItem和 back action ,您可以通过 Storyboard 将Semantic更改为Force-Right-to-Left来更改它。 or by code below:或通过以下代码:

Swift 5斯威夫特 5

inside your ViewController viewDidLoad() :在您的ViewController viewDidLoad()

self.navigationController?.navigationBar.semanticContentAttribute = .forceRightToLeft

If You want to change navigation bar and the navigation controller flow to right to left direction you can use like:如果您想将导航栏和导航控制器流向从右到左的方向更改,您可以使用:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.semanticContentAttribute = .forceRightToLeft
}

But you should mention that it wont change Swipe Direction , It only effect on navigationBar, If you want to change the Swipe Direction then you can use:但是你应该提到它不会改变Swipe Direction ,它只影响导航栏,如果你想改变Swipe Direction那么你可以使用:

self.navigationController?.view.semanticContentAttribute = .forceRightToLeft

If you want to do all of them in one line and change the whole app RTL use:如果您想在一行中完成所有操作并更改整个应用程序 RTL,请使用:

UIView.appearance().semanticContentAttribute = .forceRightToLeft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM