Search code examples
swiftbuttonswiftuilabeltoolbar

Why the Label text disappear when adding its View in a toolbar?


I created a View that displays a button which has a Label with name and icon.

struct NewWordButtonView: View {

    @State var isAlert: Bool = false

    var body: some View {
        Button {
            isAlert.toggle()
        } label: {
            Label("change word", systemImage: "arrow.triangle.2.circlepath").fixedSize()
        }
    .buttonStyle(.bordered)
    }
}

And it should look like this:

enter image description here

But when I add the View in a toolbar as a toolbar item the text disappear and only the icon remains. Like this:

enter image description here

Any suggestion?


Solution

  • Set titleAndIcon label style to your label, this will make sure to show both title and icon of the label.

    Label("change word", systemImage: "arrow.triangle.2.circlepath")
        .labelStyle(.titleAndIcon)
        .fixedSize()