当我的应用在 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/