【问题标题】:Fill safe area with color用颜色填充安全区域
【发布时间】:2021-10-25 00:54:18
【问题描述】:

我想用一种颜色填充剩余的空白区域,但我找不到任何方法,而且我是 ios 开发的初学者

我的应用照片:

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

将您的内容放入ZStack 并使用以.ignoresSafeArea(.all) 作为背景的视图。

struct MainView: View {
    var body: some View {
        ZStack {
            GradientBackground(colors: [.red, .green])
                .edgesIgnoringSafeArea(.all)
            VStack(alignment: .leading) {
                HStack {
                    Text("Setttings")
                        .foregroundColor(.white)
                        .font(.title)
                        .bold()
                    Spacer()
                }
                .padding(16)
                .background(Rectangle().fill(Color.blue))
                Spacer()
            }
        }
    }
}

struct GradientBackground: View {
    let colors: [Color]
    var body: some View {
        LinearGradient(gradient: .init(colors: colors), startPoint: .top, endPoint: .bottom)
    }
}

【讨论】:

    【解决方案2】:

    在视图上使用属性 .ignoresSafeArea() 可以将设置视图的边界扩展到安全区域之外。

    文档可以在here找到

    【讨论】:

    • 但这也可能会影响子视图并阻止 TextField 尊重它,更准确地说是底部的视图并最终被键盘覆盖。
    猜你喜欢
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多