.lineLimit does not work on Mac Menu Bar - SwiftUI

Viewed 53

I have this view for a mac menu bar app which produces the image below.

VStack {
    Text("Title of App").font(.title).frame(alignment: .center)
    List(results, id: \.title) { item in
    VStack(alignment: .leading) {
                    Text(item.title)
                        .font(.headline)
                    Text(item.description)
     }
}

enter image description here

I want to make it so the Text containing item.description is more than one line which can be done using lineLimit but when I apply it to an element it remains exactly the same. I also tried using .fixedSize(horizontal: false, vertical: true) first on the item.description and then on the VStack it belongs to which produced the following result.

enter image description here

I'm running MacOS 10.5.3 with XCode version 11.6

1 Answers

Here is a solution (tested with Xcode 11.4 / macOS 10.15.6)

VStack {
    Text("Title of App").font(.title).frame(alignment: .center)

    // use geometry reader to fix list content width
    GeometryReader { gp in
       List(results, id: \.title) { item in
         VStack(alignment: .leading) {
            Text(item.title)
                .font(.headline)
            Text(item.description)
         }
         .frame(width: gp.size.width)   // << here !!
       }
   }
}
Related