Update navigation bar back button image for the wh

2019-09-17 05:16发布

问题:

I need to update the navigation bar back button image for the whole project some sort I achieved this from the code below, But the problem is back button is showing two adjacent lines which are un wanted for me. Please guide me is there any code to remove those lines.

let image = UIImage(named:"backButton")
        let backImage = image?.imageWithRenderingMode(.AlwaysOriginal).resizableImageWithCapInsets(UIEdgeInsetsMake(0, 13, 0, 6))

        UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImage, forState: .Normal, barMetrics: .Default)
        UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-400, 0), forBarMetrics: .Default)

回答1:

These 2 lines are parts of the distorted backButton image that appear because of the incorrect cap insets. Setting caps to cover the whole image would help. e.g.:

if let image = UIImage(named:"backButton"){
    let backImage = image.withRenderingMode(.alwaysOriginal).resizableImage(withCapInsets: UIEdgeInsetsMake(image.size.height, image.size.width, 0, 0))
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImage, for: .normal, barMetrics: .default)
    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-400, 0), for: .default)
}

(Swift 3)



回答2:

Add the bar button item on the view at storyboard, - click bar button item,
- choose attributes inspector,
- under Bar Item section,
- you can select image or define others.