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

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() {

        // 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)
        // 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)

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



Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.