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

Hacking with macOS - Project 12 - Animation

Forums > macOS

I have started subject project and I am not seeing the red background as mentioned on page 457.

It appears this won't affect the rest of the app, but I wanted to understand what this difference is. I'm using XCode 11.4.1 and macOS Catalina 10.15.4

Here's the code:

override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        imageView = NSImageView(image: NSImage(named: "penguin")!)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.frame = CGRect(x: 272, y: 172, width: 256, height: 256)
        view.addSubview(imageView)
        // According to the book, this should create a red background, but it does not.
        imageView.layer?.backgroundColor = NSColor.red.cgColor
        let button = NSButton(title: "Click Me", target: self, action: #selector(animate))
        button.frame = CGRect(x: 10, y: 10, width: 100, height: 30)
        view.addSubview(button)
    }

It works if I force it using the imageView.wantsLayer = true before the background color line.

Tom

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.