LazyVGrid is offset when using NSApplicationDelegateAdaptor

Hello! I'm having a rough time getting the layout of LazyVGrid to work properly when using NSApplicationDelegateAdaptor. My SwiftUI ContentView is introduced through the AppDelegate with an NSWindow and it seems that SwiftUI does not initially recognize the titlebar, tabview, toolbar items. It does after scrolling. I've tried working around it with removing the safe zone and adding a buffer to both sides, having the height computed but bugs arise visually here and performance seems to drop. Any fixes possible? I recognize this is in beta still.

Heres some of the attempt, it does function to an extent
Code Block swift
class TitlebarCoordinator: ObservableObject {
    @Published var height: CGFloat
    init(initial: CGFloat) {
        height = initial
    }
}
var titlebarCoordinator = TitlebarCoordinator(initial: 54)
extension NSWindow {
    var titlebarHeight: CGFloat {
        let titleHeight: CGFloat = frame.height - contentLayoutRect.height
        let tabHeight: CGFloat = 30
        guard self.tabbedWindows?.count ?? 0 > 1 else {
            return titleHeight
        }
        return titleHeight + 0
    }
}
extension AppWindow: NSWindowDelegate, NSTabViewDelegate {
    func windowDidResize(_ notification: Notification) {
        print("window resized")
        titlebarCoordinator.height = titlebarHeight
    }
    func windowWillClose(_ notification: Notification) {
        windowDidResize(Notification(name: Notification.Name(rawValue: "didResize")))
    }
}

Padding and offset then have to be set to titlebarCoordinator.height for the view

Another near solution is to flip the view twice and scroll to the bottom but the alignment is off

Answered by neptunes in 617532022
Alrighty, I think I've got a temp fix. I think the problem is that when initiated a view somewhere in the hierarchy of SwiftUI with LazyVGrid doesn't recognize those AppKit elements but then does once refreshed. So to refresh, flipping the view helps, but it has to be done 2 times to get it back to normal 😆. Not sure if the have to be positioned on a specific view but add the following extension.

Code Block swift
extension View {
    public func flip() -> some View {
        return self
            .rotationEffect(.radians(.pi))
            .scaleEffect(x: -1, y: 1, anchor: .center)
    }
}


Here is my view implementation

Code Block swift
        VStack(alignment: .trailing, spacing: 0) {
        ScrollView {
            ScrollViewReader { reader in
                ZStack {
                    Rectangle().foregroundColor(.blue)
                    ListView()
                }
            }
        }.flip()
        .flip()
        }


And cross your fingers it works.

Accepted Answer
Alrighty, I think I've got a temp fix. I think the problem is that when initiated a view somewhere in the hierarchy of SwiftUI with LazyVGrid doesn't recognize those AppKit elements but then does once refreshed. So to refresh, flipping the view helps, but it has to be done 2 times to get it back to normal 😆. Not sure if the have to be positioned on a specific view but add the following extension.

Code Block swift
extension View {
    public func flip() -> some View {
        return self
            .rotationEffect(.radians(.pi))
            .scaleEffect(x: -1, y: 1, anchor: .center)
    }
}


Here is my view implementation

Code Block swift
        VStack(alignment: .trailing, spacing: 0) {
        ScrollView {
            ScrollViewReader { reader in
                ZStack {
                    Rectangle().foregroundColor(.blue)
                    ListView()
                }
            }
        }.flip()
        .flip()
        }


And cross your fingers it works.

LazyVGrid is offset when using NSApplicationDelegateAdaptor
 
 
Q