ios - SwiftUI 将 Label 的垂直对齐方式更改为居中图像和文本
问题描述
我试图将 SwiftUI 标签的图像和文本垂直居中。我试图覆盖图像的alignmentGuide,但它没有用。有任何想法吗?
Label {
Text("Test fdasf \n adfsa dsfsd f asdf \n asd fasd fads sad fda")
} icon: {
Image(systemName: true ? "checkmark.circle.fill" : "circle")
.alignmentGuide(VerticalAlignment.top) {
$0[VerticalAlignment.center]
}
}
解决方案
我们可以使用自定义标签样式来做到这一点,比如
struct DemoStyle: LabelStyle {
func makeBody(configuration: Configuration) -> some View {
HStack(alignment: .center) { // << here !!
configuration.icon
configuration.title
}
}
}
并使用它
Label {
Text("Test fdasf \n adfsa dsfsd f asdf \n asd fasd fads sad fda")
} icon: {
Image(systemName: true ? "checkmark.circle.fill" : "circle")
}
.labelStyle(DemoStyle())
使用 Xcode 13 / iOS 15 测试
推荐阅读
- swift - 无法从 Catalina 上的 Today 小部件启动包含应用程序
- r - 如何对由多个分组变量分隔的个体进行多个变量的方差分析?
- php - mgp25 Instagram-API 无法再上传
- java - 为什么我的 while 循环会导致 java 控制台不出现?
- java - HashTable 和 LinkedList 的 LinkedHashSet 实现?
- c - 在 int 列表中弹出不需要的元素
- go - 无法处理 goroutine 死锁
- reactjs - 如何动态选择 Material-UI 自动完成组件中的默认选项?
- java - 测试方法的自定义运行程序,而不是在类级别 - 可能吗?
- javascript - 页面正在通过 Otp 提交而不刷新?