Skip to content

Instantly share code, notes, and snippets.

@embassem
Created February 28, 2019 15:21
Show Gist options
  • Select an option

  • Save embassem/1bfde0c817e9c145cebe2c990f2a70c9 to your computer and use it in GitHub Desktop.

Select an option

Save embassem/1bfde0c817e9c145cebe2c990f2a70c9 to your computer and use it in GitHub Desktop.
Customize BackBarButton Image
// in App Delegate Call self.changeBackBarButtonImage()
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// customize Navigation back Button Image
self.changeBackBarButtonImage()
return true
}
}
import UIKit
// In BaseViewController . or in Every ViewController Call self.navigationItem.clearBackBarButtonTitle()
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.clearBackBarButtonTitle()
}
}
import UIKit
extension UIApplicationDelegate {
func changeBackBarButtonImage(){
guard var backButtonBackgroundImage:UIImage = UIImage(named: "ic_nav_back") else {return}
// The background should be pinned to the left and not stretch.
backButtonBackgroundImage = backButtonBackgroundImage
.resizableImage(withCapInsets: UIEdgeInsets(top: 0,
left: -1,
bottom: 0,
right: 0))
let barAppearance = UINavigationBar.appearance(whenContainedInInstancesOf:[UINavigationController.self])
// var barAppearance = UINavigationBar.appearance(whenContainedInInstancesOf:[AppNavigationController.self])
barAppearance.backIndicatorImage = backButtonBackgroundImage
barAppearance.backIndicatorTransitionMaskImage = backButtonBackgroundImage
}
}
extension UINavigationItem {
func clearBackBarButtonTitle(){
// Provide an empty backBarButton to hide the 'Back' text present by
// default in the back button.
//
// NOTE: You do not need to provide a target or action. These are set
// by the navigation bar.
// NOTE: Setting the title of this bar button item to ' ' (space) works
// around a bug in iOS 7.0.x where the background image would be
// horizontally compressed if the back button title is empty.
let backBarButton: UIBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
self.backBarButtonItem = backBarButton
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment