WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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.

1      

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

2      

@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?

1      

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.

2      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.