Learn how to style buttons to use custom images, and style sliders to use custom thumb images.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3990-your-first-swift-4-ios-11-app/lessons/40
Learn how to style buttons to use custom images, and style sliders to use custom thumb images.
Hi Ray,
thanks for putting together the videos - they are really helpful. I have a minor issue with this one: When doing:
let thumbImageNormal: UIImage = UIImage(named: "SliderThumb-Normal")
Xcode complains because UIImage(named:) seems to return an optional that’s not getting unwrapped. I’ve added a “!” to unwrap the optional but I’m wondering why in the video there isn’t an error while I got one.
Thanks
Lucas
Good question! The difference is you explicitly declared the variable as a UIImage:
let thumbImageNormal: UIImage = ...
Whereas in the video I declare the type implicitly:
let thumbImageNormal = ...
It turns out that UIImage(named: "SliderThumb-Normal")
returns an UIImage?
, not a UIIImage
. So my thumbImageNormal
is a UIImage?
while you declared yours as a UIImage
.
You’ll learn a lot more about optionals and how they work in the Programming in Swift course, so keep an eye out for that :]
Also having issues here, my sliders don’t look the same as yours, here is my code vs yours, can anyone see the difference?
<<<<<<<<< Mine HERE >>>>>>>>
let thumbImageNormal = UIImage(named: "SliderThumb-Normal")
slider.setThumbImage(thumbImageNormal, for: .normal)
let thumbImageHighlighted = UIImage(named: "SliderThumb-Highlighted")
slider.setThumbImage(thumbImageHighlighted , for: .highlighted)
let insets = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
let trackLeftImage = UIImage(named: "SliderTrackLeft")
let trackLeftResizable = trackLeftImage?.resizableImage(withCapInsets: insets)
slider.setMinimumTrackImage(trackLeftResizable, for: .normal)
let trackRightImage = UIImage(named: "SliderTrackRight")
let trackRightResizable = trackRightImage?.resizableImage(withCapInsets: insets)
slider.setMinimumTrackImage(trackRightResizable, for: .highlighted)
*/
// >>>>>>>>>>>>>>>>>> yours HERE <<<<<<<<<<<<<<<<<<<<<
let thumbImageNormal = UIImage(named: "SliderThumb-Normal")
slider.setThumbImage(thumbImageNormal, for: .normal)
let thumbImageHighlighted = UIImage(named: "SliderThumb-Highlighted")
slider.setThumbImage(thumbImageHighlighted, for: .highlighted)
let insets = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
let trackLeftImage = UIImage(named: "SliderTrackLeft")
let trackLeftResizable = trackLeftImage?.resizableImage(withCapInsets: insets)
slider.setMinimumTrackImage(trackLeftResizable, for: .normal)
let trackRightImage = UIImage(named: "SliderTrackRight")
let trackRightResizable = trackRightImage?.resizableImage(withCapInsets: insets)
slider.setMaximumTrackImage(trackRightResizable, for: .normal)
apart from setMaximumTrackImage and setMinimumTrackImage I can’t see anything else wrong…
In yours it looks like you call setMinimumTrackImage()
twice, but in mine I call setMinimumTrackImage()
for the left image, and setMaximumTrackImage()
for the right image. Maybe that is the issue?
Hi Ray
I noticed that and changed it, but there must be an other bug as it still didn’t work. I copied and pasted your code into the project to be able to keep following along.
This topic was automatically closed after 166 days. New replies are no longer allowed.
Hi I have a problem with this part of the course. See the screenshot I made.
For some reason when I type “slider.setThumbImage” xcode does not auto compleet and it looks like it’s not recognized. Also I have a lot of errors. I tried to copy and past in the code of Ray but this gives the same result. Can someone give me some help?
I’ll found the error in a few minutes today. It was a missing “}” I suppose it wat to late yesterday and I was tired after a day of work.