0

我正在尝试将图像与 Swift/SwiftUI 中的屏幕中心对齐。

GeometryReader { geometry in
            HStack {
                Spacer()
                Color.white
                    .ignoresSafeArea()
                Image("1Pzpe")
                    .frame(width: geometry.size.width-3, height: geometry.size.width-3, alignment: .center)
                Spacer()
            }

在此处输入图像描述 而不是居中,我的图像向右移动了一点。任何帮助将不胜感激 :)

4

1 回答 1

0

对于这种情况,您可能不需要 GeometryReader

您将颜色视图放在图像旁边,因此它永远不会居中,如果您希望图像超过颜色,您可能只需要一个 ZStack

ZStack{
        Color.white
            .ignoresSafeArea()
        Image(systemName: "iphone")
    }

要在视图中居中图像,您只需将图像放在那里也不需要 GeometryReader 来填充图像,只需使用.resizable() .scaledToFit().padding(3)

ZStack{
        Color.white
            .ignoresSafeArea()
        Image(systemName: "square.fill")
            .resizable()
            .scaledToFit()
            .padding(3)
            .foregroundColor(.red)
    }
于 2022-02-20T00:58:49.873 回答