Search code examples
iosswiftuibarbuttonitem

Safari-style back button


I've not found on stackoverflow a modern solution how to make a Safari-style back button for UIBarButtonItem. Is there a simple and elegant way to implement a back button as it is in Safari?

Safari toolbar screenshot


Solution

  • If you are targeting iOS13 and above, you can use the built-in SF Symbols library provided by Apple.

    The chevron.left image is used for the Safari back button.

    let chevronLeft = UIImage(systemName: "chevron.left")
    let backButton = UIBarButtonItem(image: chevronLeft, style: .plain, target: webView, action: #selector(webView!.goBack))
    

    These icons also come in nine weights from ultralight to black, which can be applied like this.

    let chevronLeft = UIImage(systemName: "chevron.left", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))
    

    More information is available from Apple here: SF Symbols