Why my TableColumn in the Table doesn't resize dynamically when I change the length of the string and width property set to "infinity"?

3 weeks ago 27
ARTICLE AD BOX

In SwiftUI, .width(min:ideal:max:) doesn't automatically make the column scrollable or dynamically resize beyond the parent container.

.infinity in max means take all available space, not grow beyond parent if content is longer.

Your Table is inside a VStack constrained by .frame(width: 400), so SwiftUI assumes the table cannot exceed 400 points width. Hence, no horizontal scrolling occurs.

You need to wrap the Table in a scroll view with .horizontal enabled & remove the fixed .frame(width:) (or let the scroll view handle overflow). Optionally you can set a minimum width for the column to let it shrink/expand naturally.

Your final code will be

import SwiftUI import Foundation struct myItem: Identifiable { let id = UUID() let myItemStr: String } struct ContentView: View { @State private var myTableTitleStr: String = "My Items" @State private var selectedItemID: myItem.ID? @State private var myItemsArray = [ myItem(myItemStr: "Item01"), myItem(myItemStr: "Item02"), myItem(myItemStr: "Item03") ] var body: some View { VStack { ScrollView([.horizontal, .vertical]) { // <- this will enable scrolling Table(myItemsArray, selection: $selectedItemID) { TableColumn(myTableTitleStr, value: \.myItemStr) .width(min: 50, ideal: 350) // remove .infinity } } .frame(height: 300) // limit height if needed Button("Add Long String") { let myLongString = "my very very very very very very very very very very very very very very very very very long string" myItemsArray[0] = myItem(myItemStr: myLongString) } } .padding() } }

Fahim Parkar's user avatar

1 Comment

This code doesn't work at all on either iOS or macOS (table doesn't show any data). Did you actually test it? If so, on which platform? Or was the answer just generated by AI, by any chance?

2026-04-08T07:46:59.947Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article