在 SwiftUI 中,可以通过 overlay modifier 和 LinearGradientImage 设置渐变颜色。具体代码如下:

Image('yourImage')
    .resizable()
    .aspectRatio(contentMode: .fit)
    .overlay(
        LinearGradient(gradient: Gradient(colors: [.clear, .black]), startPoint: .top, endPoint: .bottom)
            .frame(height: 100)
    )

以上代码中,先使用 resizableaspectRatio modifiers 来设置 Image 的尺寸,然后使用 overlay modifier 添加一个 LinearGradient。其中,Gradient 是渐变颜色的集合,startPointendPoint 分别是渐变的起点和终点,frame modifier 设置渐变的高度。这样,Image 的顶部会逐渐变为透明,底部会逐渐变为黑色。可以根据需要自己调整 GradientstartPointendPointframe

SwiftUI: 为Image添加渐变颜色

原文地址: https://www.cveoy.top/t/topic/muQo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录