ios - 添加 UIBarButtonItem 覆盖 UINavigationController 后退按钮

标签 ios swift uinavigationcontroller uinavigationbar uinavigationitem

当我的应用在 RTL 设备配置上运行时,我向 self.navigationItem.leftBarButtonItem 添加了一个 UIBarButtonItem,当 LTR 时,我向 self.navigationItem.rightBarButtonItem 添加了一个设备配置。 在这两个配置中添加的 UIBarButtonItem 覆盖了作为默认按钮的后退按钮。

这是我的代码:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: CGFloat(150), height: CGFloat(20)))
    label.backgroundColor = UIColor.clear
    label.font = UIFont.systemFont(ofSize: 18.0)
    label.shadowColor = UIColor(white: 0.0, alpha:0.5)
    label.textAlignment = NSTextAlignment.center
    label.textColor = UIColor(hexaValue: ConsColors.albums)
    label.text = "Just text"
    label.sizeToFit()
    if UIApplication.shared.userInterfaceLayoutDirection == UIUserInterfaceLayoutDirection.rightToLeft{
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: label)
    }else{
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: label)
    }

我什至尝试使用普通系统 UIBarButtonItem。 还尝试设置 UINavigationcontroller 语义内容属性强制 RightToLeft 或 LefttoRight,但这不起作用。

UINavigationBar.appearance().semanticContentAttribute = .forceRightToleft

我使用 Swift 3、Xcode 8.1 和 iPad Air 2,其操作系统版本为 9.3.2 有谁知道如何修复它?

感谢您的帮助,谢谢。

最佳答案

在下面设置这个属性

self.navigationItem.leftItemsSupplementBackButton = true

这将确保在后退按钮之后添加自定义按钮

更新:

据我了解,您的要求是在对面有默认的后退按钮和自定义按钮。不需要以下条件

    if UIApplication.shared.userInterfaceLayoutDirection == UIUserInterfaceLayoutDirection.rightToLeft{
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: label)
    }else{
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: label)
    }

将其替换为

self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: label)

关于ios - 添加 UIBarButtonItem 覆盖 UINavigationController 后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40805923/

相关文章:

Ios 使用正则表达式从字符串中提取数据

ios - 使用Auto Layout的iAd ADBannerView有多种外观

ios - 非法配置错误xcode6,我可以禁用它吗?

ios - 如何将 UIButton 粘贴到 UIScrollView 的底部? (Chatto 框架)

ios - 关闭搜索栏后,UINavigationBar 后退按钮始终变为蓝色

iOS:无法将 NextViewContrller 类型的值转换为 UINavigationController

ios - 更新 GPUImageFilterGroup 中的一个滤镜而不重绘所有滤镜

ios - UIScrollView 将按钮保持在固定位置

ios - UIImageView缩放图像问题

swift - 在新的导航 Controller 中嵌入 View Controller