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

时间:2017-09-25 11:47:16

标签: ios uinavigationbar uinavigationitem

我设置了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)

它工作正常但是使用默认的苹果返回按钮。

以下是出现的导航栏的屏幕截图

enter image description here

我无法得到正在发生的事情。有人可以建议我解决方法吗?感谢。

2 个答案:

答案 0 :(得分:2)

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。无需在每个控制器上创建基本控制器或编写代码。只需将这些行放在app delegate中即可。

答案 1 :(得分:0)

您可以使用此方法:

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

将其粘贴到扩展名UIViewController