SwiftUI -4- overlay

如果需要将另一个图片或文字分层放置在图片视图之上,SwiftUI提供了overlay修饰器可将一个组件与图片进行重叠

图片重叠

Image("paris")
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(width: 300)
    .clipShape(Circle())
    .overlay(
        Image(systemName: "heart.fill")
            .font(.system(size: 50))
            .foregroundColor(.black)
            .opacity(0.5)
)
16791179279166.jpg

文字控件和图片重合

Image("paris")
    .resizable()
    .aspectRatio(contentMode: .fit)
    .overlay(

        Text("If you are lucky enough to have lived in Paris as a young man, then wherever you go for the rest of your life it stays with you, for Paris is a moveable feast.\n\n- Ernest Hemingway")
            .fontWeight(.heavy)
            .font(.system(.headline, design: .rounded))
            .foregroundColor(.white)
            .padding()
            .background(Color.black)
            .cornerRadius(10)
            .opacity(0.8)
            .padding(),

        alignment: .top

    )
16791179279217.jpg

图片覆盖颜色图层(变暗)

    Image("jr9Li_iNaE0")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .overlay(
                //矩形组件
                //                Rectangle()
                //                    .fill(.black.opacity(0.4))
                //颜色组件
                Color(.black)
                    .opacity(0.4)
                //叠加一层文字
                    .overlay(
                        Text("SwiftUI")
                            .font(.system(size: 40 ,design: .rounded))
                            .fontWeight(.medium)
                            .foregroundColor(.white)
                            .frame(width: 200)
                            
                    )
                   
            )
16791179279261.jpg
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容