Update navigation bar back button image for the whole project

拟墨画扇 提交于 2019-12-25 16:38:29

问题


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.



来源:https://stackoverflow.com/questions/41690454/update-navigation-bar-back-button-image-for-the-whole-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!