'Missing rounded context menu preview when List item Text spans multiple lines
When using ContextMenu on SwiftUI List of items I am getting preview without rounded corners when Text does not fit one line. My real List uses items with more complex view, but I am able to demonstrate the issue using this simple example. I let the List layout the item View and set the height of the row based on the item content. I am not setting the item view frame here. But even when I use GeometryReader or preference modifier to read and pass the content height from the child view and set it onto the item view using frame modifier, I am still getting the same bad result. Any ideas how to fix that? Thank you!
struct ContentView: View {
@State var items = [String]()
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) { item in
Text(item)
.contextMenu {
Button(role: .destructive) {
// remove item
} label: {
Label("Remove", systemImage: "trash")
}
}
}
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
items.append(UUID().uuidString)
} label: {
Image(systemName: "plus.circle")
}
}
}
.navigationBarTitleDisplayMode(.inline)
.listStyle(.plain)
}
}
}
My attempt to set the frame after I read it using preference modifier (with no luck):
@ViewBuilder
private func framedRow(for item: String) -> some View {
if let height = sizes[item] {
row(for: item).frame(height: height)
} else {
row(for: item)
}
}
private func row(for item: String) -> some View {
Text(item)
.readSize { newSize in
print("newSize: \(newSize)")
sizes[item] = newSize.height
}
.contextMenu {
Button(role: .destructive) {
} label: {
Label("Remove", systemImage: "trash")
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|



