Creating NSStatusBar.system.statusItem generates console warning

Putting:

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

at the top of AppDelegate.swift and no other code at all generates the warning below in the console when the app runs. Looks benign but it sure would be great to not have that happen.

Anyone figure out how to stop this other than muting the warning? I tried all kinds of exotic deferring but that just makes the code brittle and unreadable.

Here's the warning:

It's not legal to call -layoutSubtreeIfNeeded on a view which is already being laid out. If you are implementing the view's -layout method, you can call -[super layout] instead. Break on void _NSDetectedLayoutRecursion(void) to debug. This will be logged only once. This may break in the future.

Xcode 26.5, 26.4 - macOS 26.5.1

I tried making a sample app matching your description, and was not able to reproduce the problem across a few permutations.

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) at top level will basically be lazily initialized at the point of first use. I suspect that is the part that is missing in my effort to reproduce.

The backtrace where _NSDetectedLayoutRecursion is hit will be instructive. That may afford enough insight for you to infer what is going on.

I'd recommend filing a feedback with a minimal reproduction case. Creating a status item like this should not hit this diagnostic, and is surprising.

Thanks for looking into this - I filed this with feedback. Here's the sample code to cause it and here's the stack. Thanks!


@main
class AppDelegate: NSObject, NSApplicationDelegate {
  
  var windowController: WindowController?
  
  let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
  
  func applicationDidFinishLaunching(_ aNotification: Notification) {
    print("applicationDidFinishLaunching")
    }

  func applicationWillTerminate(_ aNotification: Notification) {
    }

  func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
    return true
  }

![]("https://developer.apple.com/forums/content/attachment/eaae2144-7c22-42fc-8d74-c44013965756" "title=debugger.png;width=540;height=414")
}
Creating NSStatusBar.system.statusItem generates console warning
 
 
Q