为什么在 iOS 11 中 Navigation backBarButtonItem 的位置出现了三个按钮?

Why three buttons are appearing in the place of Navigation backBarButtonItem in iOS 11?

我设置了UINavigation栏的外观,如下图,

代码:

fileprivate class func barButtonAppearance() {
    var attributes = [String : AnyObject]()
    attributes[NSFontAttributeName] = UIFont(name: .Regular, size: 14)
    attributes[NSForegroundColorAttributeName] = UIColor.descriptionColor()
    UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: UIControlState())

    let backImage =  UIImage.image(assetID: .NavigationBarBack, caps: UIEdgeInsetsMake(0, 23, 0, 0)).withRenderingMode(.alwaysTemplate)
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImage, for: .normal, barMetrics: .default)

    UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
}

在 iOS 11.

测试我们的应用程序之前,它一直运行良好

如果我评论下面的代码

let backImage =  UIImage.image(assetID: .NavigationBarBack, caps: UIEdgeInsetsMake(0, 23, 0, 0)).withRenderingMode(.alwaysTemplate)
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImage, for: .normal, barMetrics: .default)

它工作正常,但使用默认的苹果后退按钮。

这是出现的导航栏的屏幕截图,

我无法了解发生了什么。有人可以建议我解决方法吗?谢谢

你可以使用这个方法:

func addBackButton() {
    let leftButton = UIBarButtonItem(image: Asset.backIco, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.popViewControllerAnimated(_:)))
    self.navigationItem.leftBarButtonItem = leftButton
}

将其粘贴到扩展 UIViewController 中

UINavigationBar.appearance().backIndicatorImage = image.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image.withRenderingMode(.alwaysOriginal)

if #available(iOS 11, *) {
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .highlighted)
} else {
    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -60, vertical: -60), for: .default)
}

图片是UIImage。无需创建基本控制器或在每个控制器上编写代码。只需将这些行放在应用程序委托中。