Post

Replies

Boosts

Views

Activity

Reply to Equal width subviews in a stack
MyEqualWidthHStack { Button { Task { await update() } } label: { Text("update") .frame(maxWidth: .infinity) } .border(.yellow) Button { Task { await sync() } } label: { Text("sync") .frame(maxWidth: .infinity) } .border(.yellow) } struct MyEqualWidthHStack: Layout { func sizeThatFits( proposal: ProposedViewSize, subviews: Subviews, cache: inout Void ) -> CGSize { // Return a size. guard !subviews.isEmpty else { return .zero } let maxSize = maxSize(subviews: subviews) let spacing = spacing(subviews: subviews) let totalSpacing = spacing.reduce(0) { $0 + $1 } return CGSize( width: maxSize.width * CGFloat(subviews.count) + totalSpacing, height: maxSize.height) } func placeSubviews( in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Void ) { // Place child views. guard !subviews.isEmpty else { return } let maxSize = maxSize(subviews: subviews) let spacing = spacing(subviews: subviews) let placementProposal = ProposedViewSize(width: maxSize.width, height: maxSize.height) var x = bounds.minX + maxSize.width / 2 for index in subviews.indices { subviews[index].place( at: CGPoint(x: x, y: bounds.midY), anchor: .center, proposal: placementProposal) x += maxSize.width + spacing[index] } } private func maxSize(subviews: Subviews) -> CGSize { let subviewSizes = subviews.map { $0.sizeThatFits(.unspecified) } let maxSize: CGSize = subviewSizes.reduce(.zero) { currentMax, subviewSize in CGSize( width: max(currentMax.width, subviewSize.width), height: max(currentMax.height, subviewSize.height)) } return maxSize } private func spacing(subviews: Subviews) -> [CGFloat] { subviews.indices.map { index in guard index < subviews.count - 1 else { return 0 } return subviews[index].spacing.distance( to: subviews[index + 1].spacing, along: .horizontal) } } } https://developer.apple.com/documentation/swiftui/composing_custom_layouts_with_swiftui
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’24
Reply to SwiftData Vector Embeddings
No. SwiftData is just a traditional O/RM (Object-Relational Mapping) framework. Default underlaying is SQLite database.
Replies
Boosts
Views
Activity
Oct ’24
Reply to Instruments - Swift Concurrency not working
Same issue here. Several days ago, it works, but now, only macOS could not show the Swift Actors and Swift Tasks.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Mar ’24
Reply to MyEqualWidthHStack WWDC example not working for me
SwiftUI 5 (iOS 17, macOS 14) MyEqualWidthHStack { Button { Task { await update() } } label: { Text("update") .frame(maxWidth: .infinity) } .border(.yellow) Button { Task { await sync() } } label: { Text("sync") .frame(maxWidth: .infinity) } .border(.yellow) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’24
Reply to Equal width subviews in a stack
MyEqualWidthHStack { Button { Task { await update() } } label: { Text("update") .frame(maxWidth: .infinity) } .border(.yellow) Button { Task { await sync() } } label: { Text("sync") .frame(maxWidth: .infinity) } .border(.yellow) } struct MyEqualWidthHStack: Layout { func sizeThatFits( proposal: ProposedViewSize, subviews: Subviews, cache: inout Void ) -> CGSize { // Return a size. guard !subviews.isEmpty else { return .zero } let maxSize = maxSize(subviews: subviews) let spacing = spacing(subviews: subviews) let totalSpacing = spacing.reduce(0) { $0 + $1 } return CGSize( width: maxSize.width * CGFloat(subviews.count) + totalSpacing, height: maxSize.height) } func placeSubviews( in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Void ) { // Place child views. guard !subviews.isEmpty else { return } let maxSize = maxSize(subviews: subviews) let spacing = spacing(subviews: subviews) let placementProposal = ProposedViewSize(width: maxSize.width, height: maxSize.height) var x = bounds.minX + maxSize.width / 2 for index in subviews.indices { subviews[index].place( at: CGPoint(x: x, y: bounds.midY), anchor: .center, proposal: placementProposal) x += maxSize.width + spacing[index] } } private func maxSize(subviews: Subviews) -> CGSize { let subviewSizes = subviews.map { $0.sizeThatFits(.unspecified) } let maxSize: CGSize = subviewSizes.reduce(.zero) { currentMax, subviewSize in CGSize( width: max(currentMax.width, subviewSize.width), height: max(currentMax.height, subviewSize.height)) } return maxSize } private func spacing(subviews: Subviews) -> [CGFloat] { subviews.indices.map { index in guard index < subviews.count - 1 else { return 0 } return subviews[index].spacing.distance( to: subviews[index + 1].spacing, along: .horizontal) } } } https://developer.apple.com/documentation/swiftui/composing_custom_layouts_with_swiftui
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’24
Reply to SwiftUI Button: Inaccurate tap area. How should I fix it?
It is the right result. The tap area is larger than the Button border. If you want to adjust the tap area, you should adjust the code. For example, use Rectangle with .onTapGesture
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’21