首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI。如何设置空间收缩行为?

SwiftUI。如何设置空间收缩行为?
EN

Stack Overflow用户
提问于 2019-12-02 09:15:08
回答 2查看 6.4K关注 0票数 13

如何在rgb视图(100 Pro )之间设置默认间距,如果它们的容器(VStack)与底部黑色视图不冲突。(如iPhone 11 Pro Max)。如果没有100 p高度的空间,但是会缩小。(就像屏幕截图中的iPhone SE )

我的代码:

代码语言:javascript
复制
struct ContentView: View {
    var body: some View {
        VStack(spacing: 0) {
            VStack(spacing: 0) {
                Rectangle()
                    .foregroundColor(.red)
                    .frame(height: 100)
                Spacer()
                    .frame(minHeight: 10, maxHeight: 100)
                Rectangle()
                    .foregroundColor(.green)
                    .frame(height: 100)
                Spacer()
                    .frame(minHeight: 10, maxHeight: 100)
                Rectangle()
                    .foregroundColor(.blue)
                    .frame(height: 100)
            }
            Spacer()
                .frame(minHeight: 10, maxHeight: 600)
            Rectangle() // keyboard
                .frame(height: 200)
        }
    }
}

所以问题是:间隔与maxHeight: 100有高度= 10 (而不是100)在iPhone 11专业麦克斯。(,但黑色视图和VStack之间的空间允许)

如何做出我解释过的行为?

EN

回答 2

Stack Overflow用户

发布于 2019-12-02 09:25:30

您需要将idealHeight.fixedSize修饰符一起用于Spacers:

代码语言:javascript
复制
Spacer()
    .frame(minHeight: 10, idealHeight: 100, maxHeight: 600)
    .fixedSize()
票数 11
EN

Stack Overflow用户

发布于 2020-12-14 02:42:45

使用空间(minLength: 10)作为最后一个间隔。

代码语言:javascript
复制
struct ContentView: View {
    var body: some View {
        VStack(spacing: 0) {
            VStack(spacing: 0) {
                Rectangle()
                    .foregroundColor(.red)
                    .frame(height: 100)
                Spacer()
                    .frame(minHeight: 10, maxHeight: 100)
                Rectangle()
                    .foregroundColor(.green)
                    .frame(height: 100)
                Spacer()
                    .frame(minHeight: 10, maxHeight: 100)
                Rectangle()
                    .foregroundColor(.blue)
                    .frame(height: 100)
            }
            Spacer(minLength: 10)
            Rectangle() // keyboard
                .frame(height: 200)
        }
    }
}

代码中的问题是,当您将一个空格包在像Spacer().frame(minHeight: 10, maxHeight: 600)这样的框架中时,它首先被认为是一个框架,然后是该帧中的一个空格。框架具有与其他视图相同的默认布局优先级。因此,父级将提出与内部VStack相同的空间。通过移除框架修饰符,空格有最少的布局优先级,因此内部VStack将占用尽可能多的空间,除了间隔所声称的最小10点和矩形的200点。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59135719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档