Post

Replies

Boosts

Views

Activity

How to align views in a LazyVGrid to a common base-line?
I have the following snippet (but you can see my entire code in GitHub, if you want): LazyVGrid(columns: columns) { ForEach(books) { book in BookView(book: book) .draggable(Book.BookTransferable(persistanceIdentifier: book.id)) } } and BookView is: VStack { Image(nsImage: book.image) .resizable() .frame(width: 150, height: 200) .scaledToFill() Text(book.title) .lineLimit(1) .font(.headline) HStack { ForEach(book.tags.sorted(), id: \.self) { tag in TagView(tag: tag, showText: false) } } } .padding() This will render each BookView on a different base-line because of the fact that the Text view sometimes takes 1, 2 or even 3 lines (as shown). How can I have all BookViews alligned at a common base-line (as it would if Text would only take one line, for example)?
0
0
48
Feb ’26
SwiftUI #Previews: How to populate with data
Suppose you have a view: struct Library: View { @State var books = [] var body: some View { VStack { ... Button("add Book") { .... } } } Such that Library view holds a Book array that gets populated internally via a button that opens a modal – or something alike. How can I use #Peviews to preview Library with a pre-populated collection? Suppose I have a static books – dummy – array in my code just for that; still, what's the best way to use that to preview the Library view?
4
0
258
Sep ’25
How to await inside SwiftUI a #Preview?
In order to setup a preview, I need to create a Book; to do that, I need to await a function – however, one cannot await inside a Preview: import SwiftUI struct BookView: View { let book: Book var body: some View { VStack { Image(book.thumbnail, scale: 1.0, label: Text(book.title)) } } } #Preview { let url = URL(filePath: "/Users/dan/Documents/Curs confirmare RO.pdf")! // 👇 here, `createBook` should be awaited; but how? let book = createBook(for: url, of: CGSize(width: 254, height: 254), scale: 1.0) BookView(book: book) }
5
0
535
Mar ’25
How do I obtain the preview image for a PDF?
I have a SwiftUI view of the form struct ContentView: View { // ... .onDrop(of: [.pdf], isTargeted: $isDropTargeted) { pdfs in for pdf in pdfs { I'm just not sure what to do next, I see there's a loadPreviewImage() that if I use like: Task.detached { // returns any NSSecureCoding object let image = try! await pdf.loadPreviewImage() } Not sure how I'm supposed to get my preview image from that NSSecureCoding object
Topic: UI Frameworks SubTopic: SwiftUI
0
0
255
Jan ’25
Does code at 04:41 compile?
The code for @State doesn't seem to work. struct DonutListView: View { var donutList: DonutList @State private var donutToAdd: Donut? var body: some View { List(donutList.donuts) { DonutView(donut: $0) } Button("Add Donut") { donutToAdd = Donut() } .sheet(item: $donutToAdd) { // <-- would need a "donut in" TextField("Name", text: $donutToAdd.name) // <-- donutToAdd is optional and I'm not sure how it would be unwrapped Button("Save") { donutList.donuts.append(donutToAdd) donutToAdd = nil } Button("Cancel") { donutToAdd = nil } } } } Does anyone have a fix for this? Thanks, Dan!
1
2
1k
May ’24
How to create and advertise a Bonjour service with Network.framework?
I'm trying to create and advertise a Bonjour service via Network.framework. In Xcode, target, Info tab, I've added the entry to plist: And then written the following code: guard let listener = try? NWListener(service: .init(name: "My Printer Service", type: "_printer._tcp"),using: .tcp) else { return nil } self.listener = listener listener.stateUpdateHandler = { newState in switch newState { case.ready: print("starting") case .failed(let error): print("error: \(error)") default: print(newState) } } listener.start(queue: .main) However, the status is failed with error message: POSIXErrorCode(rawValue: 22): Invalid argument
2
0
1.6k
Nov ’23
SwiftUI: List selection of custom struct
I want to have a single selection in a List, holding content of a custom struct: struct ChannelInfo: Hashable {     let title: String     let description: String } And the view: struct JoinChannelView: View {     @Binding var rooms: [ChannelInfo]     @State var selectedRoom: String?     var body: some View {         VStack {             if $rooms.isEmpty {                 Text("Loading...")                     .transition(.slide)             } else {                 List(selection: $selectedRoom) {                     ForEach(rooms, id: \.title) { room in                         Text(room.title)                             .font(.title)                         Text(room.description)                             .font(.subheadline)                     }                     .padding(.bottom)                 }             }         }         .frame(width: 150, height: 300)     } } My problem here is, that because inside the ForEach I render two Text views, both are selectable, but that's incorrect, I only want the title, the first Text to be selectable.
2
0
1.8k
Sep ’22
SwiftUI: How to animate List when being populated
I want to display a simple message while waiting for a list to be populated. Something like Loading... and when the list starts being populated with even one item, remove the message, via a transition, and display the list. My current attempt, that does not work, looks like: struct JoinChannelView: View {     @Binding var rooms: [ChannelInfo]     @State var selectedRoom: String?     var body: some View {         VStack {             if $rooms.isEmpty {                 Text("Loading...")                     .transition(.slide)             } else {                 List(selection: $selectedRoom) {                     ForEach(rooms, id: \.title) { room in                         Text(room.title)                             .font(.title)                         Text(room.description)                             .font(.subheadline)                     }                     .padding(.bottom)                 }             }         }         .frame(width: 150, height: 300)     } }
1
0
6.9k
Sep ’22
How to align views in a LazyVGrid to a common base-line?
I have the following snippet (but you can see my entire code in GitHub, if you want): LazyVGrid(columns: columns) { ForEach(books) { book in BookView(book: book) .draggable(Book.BookTransferable(persistanceIdentifier: book.id)) } } and BookView is: VStack { Image(nsImage: book.image) .resizable() .frame(width: 150, height: 200) .scaledToFill() Text(book.title) .lineLimit(1) .font(.headline) HStack { ForEach(book.tags.sorted(), id: \.self) { tag in TagView(tag: tag, showText: false) } } } .padding() This will render each BookView on a different base-line because of the fact that the Text view sometimes takes 1, 2 or even 3 lines (as shown). How can I have all BookViews alligned at a common base-line (as it would if Text would only take one line, for example)?
Replies
0
Boosts
0
Views
48
Activity
Feb ’26
SwiftUI #Previews: How to populate with data
Suppose you have a view: struct Library: View { @State var books = [] var body: some View { VStack { ... Button("add Book") { .... } } } Such that Library view holds a Book array that gets populated internally via a button that opens a modal – or something alike. How can I use #Peviews to preview Library with a pre-populated collection? Suppose I have a static books – dummy – array in my code just for that; still, what's the best way to use that to preview the Library view?
Replies
4
Boosts
0
Views
258
Activity
Sep ’25
Xcode sandboxing: how to allow Documents folder access?
How do I gain access to the Documents folder? Under targets, "signing and capabilities", App Sandbox, I can see the Music folder, Desktop... but not Documents.
Replies
2
Boosts
0
Views
149
Activity
May ’25
How to await inside SwiftUI a #Preview?
In order to setup a preview, I need to create a Book; to do that, I need to await a function – however, one cannot await inside a Preview: import SwiftUI struct BookView: View { let book: Book var body: some View { VStack { Image(book.thumbnail, scale: 1.0, label: Text(book.title)) } } } #Preview { let url = URL(filePath: "/Users/dan/Documents/Curs confirmare RO.pdf")! // 👇 here, `createBook` should be awaited; but how? let book = createBook(for: url, of: CGSize(width: 254, height: 254), scale: 1.0) BookView(book: book) }
Replies
5
Boosts
0
Views
535
Activity
Mar ’25
How do I obtain the preview image for a PDF?
I have a SwiftUI view of the form struct ContentView: View { // ... .onDrop(of: [.pdf], isTargeted: $isDropTargeted) { pdfs in for pdf in pdfs { I'm just not sure what to do next, I see there's a loadPreviewImage() that if I use like: Task.detached { // returns any NSSecureCoding object let image = try! await pdf.loadPreviewImage() } Not sure how I'm supposed to get my preview image from that NSSecureCoding object
Topic: UI Frameworks SubTopic: SwiftUI
Replies
0
Boosts
0
Views
255
Activity
Jan ’25
Does code at 04:41 compile?
The code for @State doesn't seem to work. struct DonutListView: View { var donutList: DonutList @State private var donutToAdd: Donut? var body: some View { List(donutList.donuts) { DonutView(donut: $0) } Button("Add Donut") { donutToAdd = Donut() } .sheet(item: $donutToAdd) { // <-- would need a "donut in" TextField("Name", text: $donutToAdd.name) // <-- donutToAdd is optional and I'm not sure how it would be unwrapped Button("Save") { donutList.donuts.append(donutToAdd) donutToAdd = nil } Button("Cancel") { donutToAdd = nil } } } } Does anyone have a fix for this? Thanks, Dan!
Replies
1
Boosts
2
Views
1k
Activity
May ’24
How to create and advertise a Bonjour service with Network.framework?
I'm trying to create and advertise a Bonjour service via Network.framework. In Xcode, target, Info tab, I've added the entry to plist: And then written the following code: guard let listener = try? NWListener(service: .init(name: "My Printer Service", type: "_printer._tcp"),using: .tcp) else { return nil } self.listener = listener listener.stateUpdateHandler = { newState in switch newState { case.ready: print("starting") case .failed(let error): print("error: \(error)") default: print(newState) } } listener.start(queue: .main) However, the status is failed with error message: POSIXErrorCode(rawValue: 22): Invalid argument
Replies
2
Boosts
0
Views
1.6k
Activity
Nov ’23
Apple Music "Automatic Downloads" broken on Sonoma beta2
Apple Music's "Automatic Downalods" checkbox does not stay checked when saving settings and does not work at all. Note this was broken for me for a long, long, time (Catalina?).
Replies
0
Boosts
0
Views
511
Activity
Jun ’23
SwiftUI: List selection of custom struct
I want to have a single selection in a List, holding content of a custom struct: struct ChannelInfo: Hashable {     let title: String     let description: String } And the view: struct JoinChannelView: View {     @Binding var rooms: [ChannelInfo]     @State var selectedRoom: String?     var body: some View {         VStack {             if $rooms.isEmpty {                 Text("Loading...")                     .transition(.slide)             } else {                 List(selection: $selectedRoom) {                     ForEach(rooms, id: \.title) { room in                         Text(room.title)                             .font(.title)                         Text(room.description)                             .font(.subheadline)                     }                     .padding(.bottom)                 }             }         }         .frame(width: 150, height: 300)     } } My problem here is, that because inside the ForEach I render two Text views, both are selectable, but that's incorrect, I only want the title, the first Text to be selectable.
Replies
2
Boosts
0
Views
1.8k
Activity
Sep ’22
SwiftUI: How to animate List when being populated
I want to display a simple message while waiting for a list to be populated. Something like Loading... and when the list starts being populated with even one item, remove the message, via a transition, and display the list. My current attempt, that does not work, looks like: struct JoinChannelView: View {     @Binding var rooms: [ChannelInfo]     @State var selectedRoom: String?     var body: some View {         VStack {             if $rooms.isEmpty {                 Text("Loading...")                     .transition(.slide)             } else {                 List(selection: $selectedRoom) {                     ForEach(rooms, id: \.title) { room in                         Text(room.title)                             .font(.title)                         Text(room.description)                             .font(.subheadline)                     }                     .padding(.bottom)                 }             }         }         .frame(width: 150, height: 300)     } }
Replies
1
Boosts
0
Views
6.9k
Activity
Sep ’22
XCode doesn't show local variables in debug
In XCode, if I hit a breakpoint, I can't view the value of some local variables: I've read about disabling optimisations for debugging, but that's already done
Replies
1
Boosts
1
Views
2.7k
Activity
Mar ’22
SwiftUI stack navigation on macOS?
Currently NaviagitonView on macOS creates a side panel. And then navigationViewStyle does not support stack view for macOS. Given this, is there currently a method to totally switch the content of a window from one view to another on macOS?
Replies
0
Boosts
0
Views
1.3k
Activity
Feb ’22
In SwiftUI 3 how do I transition between screens on macOS?
For iOS NavigationLinks provide a good way to completely transition from one "screen" (view) to another. In macOS however, NavigationLinks don't completely switch the whole contents of a screen to another. What I want to achieve is to make a Button press trigger a transition from one screen to another.
Replies
0
Boosts
0
Views
601
Activity
Dec ’21