Tru*_*an1 8 accessibility swiftui
我有一个图像,我正在对尺寸进行硬编码,但意识到它不能针对较大尺寸的类别进行缩放。如何设置首选尺寸并让它自动缩放为不同尺寸?
这就是我的代码的样子:
HStack(alignment: .top, spacing: 4) {
Text("Some text")
Button(action: { showAlert = true }) {
Image(systemName: "questionmark.circle.fill")
.resizable()
.frame(width: 12, height: 12)
.foregroundColor(.secondary)
}
}
Run Code Online (Sandbox Code Playgroud)
我还有其他不使用 SF 符号的情况:
Button(action: action) {
Label(
title: {
Text(title)
.foregroundColor(Color(.label))
},
icon: {
Image("twitter")
.resizable()
.frame(width: 24, height: 24)
}
)
}
Run Code Online (Sandbox Code Playgroud)
这是不同尺寸预览时的样子,但在较大尺寸下图像很小。我该如何处理这个问题以实现可访问性?
Asp*_*eri 27
ScaledMetricSwiftUI 2.0为此目的而提供。
这是解决方案的演示。使用 Xcode 12.1 / iOS 14.1 进行测试
struct TestScaledImage: View {
@ScaledMetric var scale: CGFloat = 1 // << here !!
var body: some View {
HStack(alignment: .top, spacing: 4) {
Text("Some text")
Button(action: { }) {
Image(systemName: "questionmark.circle.fill")
.resizable()
.frame(width: 12 * scale, height: 12 * scale) // << here !!
.foregroundColor(.secondary)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3611 次 |
| 最近记录: |