FREE TRIAL: Accelerate your app development career with 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

   

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.

Get started

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.