有没有一种方法可以让.IgnresSafeArea()起作用呢?颜色简单、居中的ZStack放置在错误的位置

edqdpe6u  于 2022-10-04  发布在  Swift
关注(0)|答案(1)|浏览(118)
struct HomeContainerView: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                ZStack {

                }
                .frame(width: geometry.size.width - 20, height: geometry.size.height - 20)
                .background(Color.orange)

                ZStack {

                }
                .frame(width: geometry.size.width - 40, height: geometry.size.height - 40)
                .background(Color.gray)

                ZStack {

                }
                .frame(width: geometry.size.width - 60, height: geometry.size.height - 60)
                .background(Color.red)
            }
            .frame(width: geometry.size.width, height: geometry.size.height, alignment: Alignment(horizontal: .center, vertical: .center))
            .background(Color.indigo)
        }
    }
}

With safe area.

只需简单地添加.InoresSafeArea(),整个屏幕的大小就不符合设备屏幕的大小了吗?

struct HomeContainerView: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                ZStack {

                }
                .frame(width: geometry.size.width - 20, height: geometry.size.height - 20)
                .background(Color.orange)

                ZStack {

                }
                .frame(width: geometry.size.width - 40, height: geometry.size.height - 40)
                .background(Color.gray)

                ZStack {

                }
                .frame(width: geometry.size.width - 60, height: geometry.size.height - 60)
                .background(Color.red)
            }
            .frame(width: geometry.size.width, height: geometry.size.height, alignment: Alignment(horizontal: .center, vertical: .center))
            .background(Color.indigo)
        }
        .ignoresSafeArea()
    }
}

Ignoring safe area.

这使得我无法使用SwiftUI设置应用程序。有没有一种方法可以忽略安全区域,使内部内容的大小等于设备的大小减去安全区域?我花了很长时间试图解开这个谜团。谢谢!

t3psigkw

t3psigkw1#

要获得您想要的结果,(至少)有三种选择:

第一种选择

视图的大小调整正确,但背景颜色绘制在框架之外。通过将.clipped()添加到每个内部ZStack,可以将背景剪裁到框架:

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                ZStack {

                }
                .frame(width: geometry.size.width - 20, height: geometry.size.height - 20)
                .background(Color.orange)
                .clipped()  // here

                ZStack {

                }
                .frame(width: geometry.size.width - 40, height: geometry.size.height - 40)
                .background(Color.gray)
                .clipped()  // here

                ZStack {

                }
                .frame(width: geometry.size.width - 60, height: geometry.size.height - 60)
                .background(Color.red)
                .clipped()  // here
            }
            .frame(width: geometry.size.width, height: geometry.size.height, alignment: Alignment(horizontal: .center, vertical: .center))
            .background(Color.indigo)
        }
        .ignoresSafeArea()
    }
}

第二种选择

或者,您可以将剪贴应用于每个背景颜色视图本身:

.background(Color.orange.clipped())

第三种选择

正如@NiravD在评论中指出的那样,您可以删除.background(color),并将该颜色作为每个内部ZStack中的第一个View

GeometryReader { geometry in
    ZStack {
        ZStack {
            Color.orange
            // additional views
        }
        .frame(width: geometry.size.width - 20, height: geometry.size.height - 20)

相关问题