ios 在ToolbarItem SwiftUI中添加带有图像和文本的按钮

kh212irz  于 2023-06-25  发布在  iOS
关注(0)|答案(2)|浏览(76)

我想按钮完全一样在提醒应用程序在iOS下面的代码说明工作,我还尝试使用标签和系统图像名称.

.toolbar {
            ToolbarItem(placement: .bottomBar) {
                Button(action: {}, label: {
                    HStack {
                        Image(systemName: "plus.circle.fill")
                        Text("New Reminder")
                    }
                })

            }
        }

voase2hg

voase2hg1#

创建自定义标签样式

struct VerticalLabelStyle: LabelStyle {
    func makeBody(configuration: Configuration) -> some View {
        VStack {
            configuration.icon.font(.headline)
            configuration.title.font(.footnote)
        }
    }
}

然后您可以使用标签的自定义样式

Button(action: {}, label: {
   Label("Home", systemImage: "house")
      .labelStyle(VerticalLabelStyle())
})

gpnt7bae

gpnt7bae2#

这对我很有效,关键是使用.buttonstyle(.plain),并将所有内容嵌入到HStack中,并使用Spacer()将按钮向右推。
Text and Icon button

.toolbar {
    ToolbarItem(placement: .bottomBar) {
        HStack {
            Button(action: { }) {
                HStack {
                    Image(systemName: "plus.circle.fill")
                        .foregroundStyle(.blue)
                    Text("Add Event")
                        .font(.system(.body, design: .rounded).bold())
                        .foregroundColor(.blue)
                    }
                }
                Spacer()
            }
            .buttonStyle(.plain)
        }

相关问题