Skip to content

Instantly share code, notes, and snippets.

@williampower
Last active November 15, 2024 01:39
Show Gist options
  • Save williampower/bacfdc1df723e02cf56c5d6c2bff0adf to your computer and use it in GitHub Desktop.
Save williampower/bacfdc1df723e02cf56c5d6c2bff0adf to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// NSWindowResizingQuestion
//
// Created by William Power on 11/14/24.
//
//This project was created to support answering a question from:
// https://stackoverflow.com/questions/79170528/nswindow-with-swiftui-content-has-zero-frame-on-macos-15
import SwiftUI
import AppKit
struct ContentView: View {
var body: some View {
VStack {
Button(action:launchWelcomeView){
Label("Launch Welcome View", systemImage: "arrow.2.circlepath.circle")
}
}
.padding()
}
}
extension ContentView {
private func launchWelcomeView() {
let otherView = WelcomeView()
let hostVC = NSHostingController(rootView: otherView)
let window = NSWindow(contentViewController: hostVC)
window.toolbar = NSToolbar()
window.makeKeyAndOrderFront(self)
NSApplication.shared.mainWindow?.windowController?.showWindow(window)
}
}
#Preview {
ContentView()
}
//This project was created to support answering a question from:
// https://stackoverflow.com/questions/79170528/nswindow-with-swiftui-content-has-zero-frame-on-macos-15
@main
struct NSWindowResizingQuestionApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
//
// WelcomeView.swift
// NSWindowResizingQuestion
//
// Created by William Power on 11/14/24.
//
//This project was created to support answering a question from:
// https://stackoverflow.com/questions/79170528/nswindow-with-swiftui-content-has-zero-frame-on-macos-15
import SwiftUI
struct WelcomeView: View {
var body: some View {
ZStack(alignment: .top) {
Text("Hello, SO")
}
//the following 2 properties set the window to a particular size
.frame(width: 600, height: 400)
.fixedSize()
.background(Color.red)
}
}
#Preview {
WelcomeView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment