UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Project 4, Challenge 2

Forums > 100 Days of Swift

Hello. I've just finished the second challenge and I had a question - the challenge states:

"Try making two new toolbar items with the titles Back and Forward. You should make them use webView.goBack and webView.goForward."

So, I attempted to do what we did with refresh, aka:

let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))

but for back and forward, so I did the below:

let back = UIBarButtonItem(barButtonSystemItem: .rewind, target: webView, action: #selector(webView.goBack))

let forward = UIBarButtonItem(barButtonSystemItem: .fastForward, target: webView, action: #selector(webView.goForward))

My main issue was with the barButtonSystemItem. I found a list of all possible system icons here but there are no simple back or forward buttons so, as per above, I've used the fast forward and rewind symbols, which work, but it's obviously not an ideal solution.

What should I have done here, or how did you guys get round it? For context, I displayed the above using:

toolbarItems = [progressButton, spacer, back, forward, refresh]

I wasn't sure if we were meant to have just the words "back" and "forward" appear, but using the method I used above it seemed that a systemitem was required.

3      

You can use a different initializer for UIBarButtonItem and supply a SFSymbol image.

let back = UIBarButtonItem(image: UIImage(systemName: "chevron.backward"),
                           style: .plain,
                           target: webView,
                           action: #selector(webView.goBack))
let forward = UIBarButtonItem(image: UIImage(systemName: "chevron.forward"),
                              style: .plain,
                              target: webView,
                              action: #selector(webView.goForward))

back + forward buttons

5      

@roosterboy - Thanks for that. Serious question, as a novice developer, how should I have found that information myself? I was alt clicking parts of the code in swift to read associated notes, and googling "UIBarButtonItem" etc, but I never stumbled across that other initialiser. For future reference, how should I have gone about that?

3      

Just by looking in the documentation for UIBarButtonItem. You could also find it by Cmd-clicking on UIBarButtonItem in your code and reading through the headers.

Once you know there is an init that takes a UIImage and that creating a UIImage by referencing a systemName refers to SFSymbols (because you've seen the same in other places), you can put 2 and 2 together and get the result I posted above.

It all just takes practice.

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.