swiftcocoaswift6macos-sequoia

NSView alignment is not working properly using Xcode 16.0


I am in macOS 15.0.1 and using Xcode 16.0 which breaks NSView alignments in my existing apps. The main problem is, the child NSView gets frame size as the parent NSView in override func draw(_ dirtyRect: NSRect) instead of 100:100

I changed swift language version 6 from 5 same problem. I've created new project as well but same problem in old and new projects. It's a problem only when using func draw(_ dirtyRect: NSRect) and it works well if I use wantsLayer=true instead of draw(_ dirtyRect: NSRect) in child view. `

How to resolve it?

private class ViewA: NSView {
    
    override func draw(_ dirtyRect: NSRect) {
        
        print("ViewA: \(dirtyRect)")
        
        NSColor.systemBlue.set()
        dirtyRect.fill()
    }
}

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
 
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        
        let v1 = ViewA()
        view.addSubview(v1)
        v1.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            
            v1.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            v1.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            v1.heightAnchor.constraint(equalToConstant: 100),
            v1.widthAnchor.constraint(equalToConstant: 100)
        ])
    }
    
    override func viewDidLayout() {
        super.viewDidLayout()
        
        print("VC.view = \(view.frame)")
    }
}

Current Output:

enter image description here

Expected Output:

let v1 = NSView()
v1.wantsLayer = true
v1.layer?.backgroundColor = NSColor.systemBlue.cgColor

enter image description here


Solution

  • As @Willeke said in comments, the solution is to make clip clipToBounds=true. It's really good to checkout AppKit release notes if anything not working expected but previously worked.