NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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



Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it now

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.