NEW: Learn to build the incredible iOS 15 Weather app today! >>

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.

   

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

1      

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

   

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.

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn 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.