图像集框架

时间:2019-06-26 10:30:54

标签: swiftui

在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)无效?

1 个答案:

答案 0 :(得分:-1)

我假设当您说“无效”时,表示图像未调整为宽度和高度为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)   
    }
}
相关问题