I am trying to determine how to set the initial position of an app window for macOS. I have tried using NSViewRepresentable and .setFrameTopLeftPoint with no success. Is my syntax incorrect to achieve the desired results or should I be looking at some other method?
struct WindowAccessor: NSViewRepresentable {
@Binding var window: NSWindow?
func makeNSView(context: Context) -> NSView {
let view = NSView()
view.window?.setFrameTopLeftPoint(CGPoint(x: 600, y: 400))
DispatchQueue.main.async {
self.window = view.window
}
return view
}
func updateNSView(_ nsView: NSView, context: Context) {}
}
and
var body: some Scene {
WindowGroup {
ContentView()
.background(WindowAccessor(window: $window))
}
Post
Replies
Boosts
Views
Activity
Hello,
I have the code below which creates a view. Unfortunately it puts a small gap between the two LazyVGrids that I would like to eliminate. Any suggestions would be appreciated.
Below is my code.
Chris
import SwiftUI
struct SecondView: View {
var columns = [
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1)
]
let principalData: convertCSVtoArray
var body: some View {
let numArrays = principalData.cvsData.count
let numElementsPerArray = principalData.cvsData[0].count
VStack{
Text("")
Text("Historical Data")
.font(.title)
.padding(5)
Divider()
LazyVGrid(
columns: columns,
alignment: .center,
spacing: 0)
{
ForEach(0..<1) {row in
ForEach(0..<numElementsPerArray) {col in
Rectangle()
.foregroundColor(.white)
.overlay (Text(principalData.cvsData[row][col]).bold())
.frame(height: 30)
.border(Color.gray, width: 2)
}
}
}
ScrollView{
LazyVGrid(
columns: columns,
alignment: .center,
spacing: 0)
{
ForEach(1..<numArrays) {row in
ForEach(0..<numElementsPerArray) {col in
Rectangle()
.foregroundColor(.white)
.overlay (Text(principalData.cvsData[row][col]))
.frame(height: 30)
.border(Color.gray, width: 0.5)
}
}
}
ForEach(0..<30){index in
Text("")
}
}// end scrollview
}
}
}
Hello,
I am trying to learn Swift. I have the code below.
With a button click I can move to a new view. What I would like to do is once the new view is displayed have the navigation link destination changed to "CurrentView" and the button text to "Previous View".
Any insight on how to resolve this issue would be appreciated.
Regards,
Chris
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: NextContentView()){
Text("Next View")
}
}
}
}
Hello,
I have a Swift program. It utilizes the SwiftUI.
I want to place my frame/container in the center of the computer screen. Any advise on how to do so would be appreciated.
Below is what I have so far.
Regards,
Chris
import SwiftUI
@main
struct Test1App: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.frame(width: 500, height: 350)
.background(Color .blue)
.padding()
}
}
Hello,
I have a swift program that allows me to transition from a primary view controller to a secondary view controller. To complete this task I create a segue between a button on the primary view controller and the secondary view controller by dragging from the button to the secondary view controller then assigning an identifier to this seque. The balance is handled programatically.
What I would like to do is eliminate the creation of the segue via dragging and handle transitioning to the secondary view controller 100% in code. I have given the secondary view controller a storyboard id so it seems logical to me that I should be able to locate the secondary view controller via this identifier and use this to transition to it.
Is there a way to do this?
Regards,
Chris