首页 > 解决方案 > 图像集框架

问题描述

在 SwiftUI 的官方教程中,我尝试自定义图像以显示整个屏幕

解决方法是将框架设置为固定的宽度和高度

struct CircleImage : View {
    var body: some View {
        Image("image01")
//            .frame(width: 300, height: 300)
            .clipShape(Circle())
            .overlay(
                Circle().stroke(Color.white, lineWidth: 4))
            .shadow(radius: 10)   
    }
}
struct ContentView : View {
    var body: some View {
        VStack {
            MapView()
                .edgesIgnoringSafeArea(.top)
                .frame(height: 300)
            CircleImage()
                .offset(y: -130)
                .padding(.bottom, -130)
                .frame(width: 300, height: 300)
            VStack(alignment: .leading) {
                Text("Hello Maxwell!")
                    .font(.title)
                HStack(alignment: .top) {
                    Text("Chaoyang Beijing")
                        .font(.subheadline)
                    Spacer()
                    Text("China")
                        .font(.subheadline)
                }
            }
            .padding()

            Spacer()
        }
    }
}

为什么 ContentView call.frame(width: 300, height: 300) 无效?

标签: swiftui

解决方案


我假设当你说“无效”时,你的意思是图像没有调整到宽度和高度为 300 的框架。如果是这样,你的问题是你没有使图像调整大小。更新CircleImage下面的代码,图像应该调整大小。

struct CircleImage : View {
    var body: some View {
        Image("image01")
            .resizable() //add this line
            .clipShape(Circle())
            .overlay(
                Circle().stroke(Color.white, lineWidth: 4))
            .shadow(radius: 10)   
    }
}

推荐阅读