ios - 如何使用 ViewModifier 在 swiftUI 中选择性地设置前景色以查看
问题描述
我有一个图像视图,基于一个布尔值,我应该使用色调,如果条件为真,我应该使用色调,否则我应该使用原始图像,我尝试了多种方式使用 ViewModifier,但我找不到解决方案。是否有可能使用 ViewModifier 获得预期的结果?
struct ViewColor: ViewModifier {
var tint: Bool
func body(content: Content) -> some View {
Group {
if self.tint {
content.foregroundColor(Color("colorText"))
}else
{
// content.foregroundColor(Color("colorAccent"))
}
}
}
}
解决方案
这是一个解决方案(使用 Xcode 12b 测试)
struct ViewColor: ViewModifier {
var tint: Bool
@ViewBuilder
func body(content: Content) -> some View {
if self.tint {
content.foregroundColor(Color("colorText"))
} else {
content
}
}
}
推荐阅读
- sql - Msn SSMS 中的动态 SQL 代码中的临时表“存在”多长时间?
- python - 使用 abs_() 的 gurobi 差异的绝对值不起作用
- java - 休眠验证不使用 Spring Boot LocalValidatorFactoryBean
- excel - 在文本框 vba 中键入时,用户窗体按钮不起作用
- python-3.x - 使用 Python 将数据写入雪花
- python - Windows 10-Python Pip 安装 pyinstaller 错误
- javascript - 使用 WebChat Hooks API 将用户名和用户 ID 参数传递给 WebChat
- python - 在 Python 中将字符串从 sha1 Hash 转换为 base 64,根据 VBA 示例返回结果
- laravel - Laravel Eloquent:一对多的关系与不同的
- node.js - Angular 通用慢速 TTFB - npm run serve:ssr