gpt4 book ai didi

ios - SwiftUI:从 ScrollView 中的底部对齐构建 View

转载 作者:行者123 更新时间:2023-12-04 01:09:15 24 4
gpt4 key购买 nike

有没有办法在带有底部对齐的 ScrollView 内的 SwiftUI 中构建 UI 元素?
我的用例:我有一个屏幕

  • 间隔( 分配下面的元素后还剩下什么 )
  • LogoView
  • 垫片() - 30
  • 一些文本 - 4/5 行
  • Spacer() - 50(这将根据 GR 大小高度计算)
  • 带有两个按钮的 HStack - 这应该固定在 View 底部/ScrollView

  • I would like to know how Can I pin the HStack view to ScrollView Bottom


    我已经复制了我的设置并在像这样的 Swift 游乐场中重现了我的问题
    struct ContentView: View {
    var body: some View {
    GeometryReader { gr in
    ScrollView {
    VStack {
    Spacer()
    Image(systemName: "applelogo")
    .resizable()
    .frame(width: gr.size.width * 0.5, height: gr.size.height * 0.3, alignment: .center)
    Spacer().padding(.bottom, gr.size.height * 0.01)
    Text("SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME")
    .fontWeight(.regular)
    .foregroundColor(.green)
    .multilineTextAlignment(.center)
    .padding(.top, gr.size.height * 0.05)
    .padding(.horizontal, 40)
    .layoutPriority(1)
    Spacer()
    .frame(minHeight: gr.size.height * 0.12)
    HStack {
    Button(action: {

    }, label: {
    Text("Button 1")
    })
    .foregroundColor(Color.white)
    .padding(.vertical)
    .frame(minWidth: 0, maxWidth: .infinity)
    .background(Color.blue)
    .cornerRadius(8)

    Button(action: {

    }, label: {
    Text("Button 2")
    })
    .foregroundColor(Color.white)
    .padding(.vertical)
    .frame(minWidth: 0, maxWidth: .infinity)
    .background(Color.blue)
    .cornerRadius(8)
    }
    .padding(.horizontal, 20)
    }
    //.frame(maxWidth: .infinity, maxHeight: .infinity)
    }
    }
    }
    }
    enter image description here
    我知道在 SwiftUI View 中引入 scrollView 时,间隔长度更改为零,想知道实现此目的的最佳方法是什么

    最佳答案

    struct SampleView: View {
    var body: some View {
    GeometryReader { gr in
    VStack {
    ScrollView {
    VStack {

    // Fills whatever space is left
    Rectangle()
    .foregroundColor(.clear)

    Image(systemName: "applelogo")
    .resizable()
    .frame(width: gr.size.width * 0.5, height: gr.size.height * 0.3, alignment: .center)
    .padding(.bottom, gr.size.height * 0.06)


    Text("SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME TEXT SOME")
    .fontWeight(.regular)
    .foregroundColor(.green)
    .multilineTextAlignment(.center)
    .padding(.horizontal, 40)
    .layoutPriority(1)


    // Fills 12 %
    Rectangle()
    .frame(height: gr.size.height * 0.12)
    .foregroundColor(.clear)

    HStack {

    Button(action: {
    }, label: {
    Text("Button 1")
    })
    .foregroundColor(Color.white)
    .padding(.vertical)
    .frame(minWidth: 0, maxWidth: .infinity)
    .background(Color.blue)
    .cornerRadius(8)

    Button(action: {
    }, label: {
    Text("Button 2")
    })
    .foregroundColor(Color.white)
    .padding(.vertical)
    .frame(minWidth: 0, maxWidth: .infinity)
    .background(Color.blue)
    .cornerRadius(8)
    }
    .padding(.horizontal, 20)
    .padding(.bottom, 20)

    }

    // Makes the content stretch to fill the whole scroll view, but won't be limited (it can grow beyond if needed)
    .frame(minHeight: gr.size.height)
    }
    }
    }
    }
    }

    关于ios - SwiftUI:从 ScrollView 中的底部对齐构建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65384646/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com