【问题标题】:Resize views imported from another view - SwiftUI调整从另一个视图导入的视图大小 - SwiftUI
【发布时间】:2021-03-16 19:45:51
【问题描述】:

我正在尝试使用来自我的数据模型和另一个视图的数据来布置视图,但我正在努力调整导入视图的大小。正如您在表格下方的图片中看到的那样,我在单独的视图中制作的图片正在接管全画幅。

我试图在我的主视图中调整它的大小,我将所有东西放在一起但没有运气。我也尝试过 scaleEffect 修饰符,但它只会缩放视图中的内容并保持视图边界区域的比例相同。

理想情况下,我想缩小表格的大小,使段落和表格之间的分割均匀。另外,对于此处共享的大量代码表示歉意,但我想分享我使用过的所有格式,以防有什么东西阻止我得到想要的结果。

下图显示在分发应用程序的 iPad 上。

这是我将所有数据汇总在一起的地方:

  struct Tab1Section08: View {
    
    var product: ProductModel
    let frameHeight: CGFloat = 900
    let title = "This is a title"
    let subtitle = "This is a subtitle"
    let paragraph = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    
    struct Tab1Section08: View {
    let frameHeight: CGFloat = 900
    let title = "This is a title"
    let subtitle = "This is a subtitle"
    let paragraph = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    
    var body: some View {
        ZStack{
            Color(red: 0.97, green: 0.97, blue: 0.97)
                .ignoresSafeArea()
                .frame(height: frameHeight)
            VStack {
                Text(title)
                    .font(.custom("Avenir-Black", size: 30))
                    .padding(.top, 60)
                    .padding(.bottom, 20)
                HStack{
                    VStack {
                        Text(subtitle)
                            .font(.custom("Avenir-Black", size: 20))
                        Text(paragraph)
                            .font(.custom("Avenir", size: 16))
                            .multilineTextAlignment(.center)
                            .fixedSize(horizontal: false, vertical: true)
                            .lineSpacing(10)
                            .padding(.bottom, 20)
                    }
                    VStack {
                        Tab1Section08ListRow()
                            .padding(.bottom, 20)
                        Text(subtitle)
                            .font(.custom("Avenir", size: 12))
                    }
                }
            }
        }
    }
}

这是我正在创建表格的视图:

    struct Tab1Section08ListRow: View {
        
        let color: Color = .gray
        let width: CGFloat = 1
        let textPadding: CGFloat = 5
        let frameWidth: CGFloat = 250
        let amount: CGFloat = 3
        let title = ["first", "second","third"]
        let columns = ["first", "second", "third", "fourth", "fifth", "sixth"]
        
        var body: some View {
            let height: CGFloat = CGFloat(title.count)*40
            VStack {
                HStack{
                    ForEach(0..<title.count, id: \.self) { item in
                        Text(title[item])
                            .font(.custom("Avenir-Heavy", size: 18))
                            .fontWeight(.bold)
                            .frame(width: frameWidth)
                            .padding(.bottom, 20)
                    }
                }
                HStack{
                    VStack(alignment: .leading) {
                        ForEach(0..<columns.count, id: \.self) { item in
                            Text(columns[item])
                                .font(.custom("Avenir", size: 14))
                                .foregroundColor(.secondary)
                                .frame(width: frameWidth)
                                .padding(.bottom, textPadding)
                                .padding(.top, textPadding)
                        }
                    }
                    Rectangle()
                        .fill(color)
                        .frame(width: width, height: height)
                        .edgesIgnoringSafeArea(.vertical)
                    VStack(alignment: .leading) {
                        ForEach(0..<columns.count, id: \.self) { item in
                            Text(columns[item])
                                .font(.custom("Avenir", size: 14))
                                .foregroundColor(.secondary)
                                .frame(width: frameWidth)
                                .padding(.bottom, textPadding)
                                .padding(.top, textPadding)
                        }
                    }
                    Rectangle()
                        .fill(color)
                        .frame(width: width, height: height)
                        .edgesIgnoringSafeArea(.vertical)
                    VStack(alignment: .leading) {
                        ForEach(0..<columns.count, id: \.self) { item in
                            Text(columns[item])
                                .font(.custom("Avenir", size: 14))
                                .foregroundColor(.secondary)
                                .frame(width: frameWidth)
                                .padding(.bottom, textPadding)
                                .padding(.top, textPadding)
                        }
                    }
                }
            }
        }
    }

【问题讨论】:

  • 您是否尝试过将每个元素乘以比例因子?
  • 是的,这行不通。对于我正在导入的视图,边界框仍然保持不变。
  • 更改堆栈中的字体?
  • 你能用一些硬连线数据设置你的代码吗?如果不知道您的产品模型是什么,就很难在 Xcode 中使用,至少就这个视图而言。我对 .frame()s 的所有固定数字有点困扰。这有时会导致意想不到的坏事发生。
  • @Yrb 我已经更新了帖子,以便可以重新创建。

标签: ios swift xcode view swiftui


【解决方案1】:

我从一开始就怀疑是你的固定框架。如果我注释掉.frame(width: frameWidth),视图就会崩溃,从而为您的“lorem ipsum”视图提供更多空间。 与.frame(width: frameWidth)

并且没有:

像这样的硬编码框架会破坏 SwiftUI 处理不同尺寸的能力。我会考虑使用几何阅读器并将宽度表示为屏幕大小的百分比。

【讨论】:

  • 是的,我在 iPod 和 iPhone 上得到了相同的结果,但我最初的问题是在 iPad 上提出的。
  • 公平地说,我只是现在才添加它以防其他人查看它...
  • 非常感谢。我真的需要了解几何阅读器并开始使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多