Last active
November 15, 2024 01:39
-
-
Save williampower/bacfdc1df723e02cf56c5d6c2bff0adf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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