首页 > 解决方案 > 在 SwiftUI 中仅显示图像的顶部

问题描述

我需要以仅显示图像顶部的方式对齐 UIImageView。

原始图像:-

截屏

我从下面的代码中获得了图像:-

截屏

代码:-

Image("ImageDemo")
  .resizable()
  .scaledToFill()
  .frame(height: 120, alignment: .center)
  .clipped()
  .cornerRadius(20, corners: [.topLeft, .topRight])
  .padding(.bottom)

有人可以向我解释如何仅显示顶部的图像,我已尝试按上述方式实现,但还没有结果。

任何帮助将不胜感激。

提前致谢。

标签: swiftimageswiftuiuiimageview

解决方案


使用alignment: .top它将解决您的问题。


struct ContentView: View {
    var body: some View {

            Image("Your Image Here!")
                .resizable()
                .scaledToFill()
                .cornerRadius(20)
                .frame(height: 120, alignment: .top) //  <<: Here
                .clipped()
                .padding()

    }
}


推荐阅读