Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

swiftui - Position view bottom without using a spacer

How can I position a View at the bottom without using a spacer. I know I can achieve it placing a spacer and my view in inside a VStack but I don't want to use a spacer because I don't want my view to take up all the vertical space. With UIKit I would position it at safe area bottom guide.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Experimenting with different approaches I finally end up with creating own custom container, which, having some known limitation, fulfils my needs completely. Hope it would be helpful for someone else.

Demo:

PinnedView Demo1

Pros: ContentView & PinnedView are absolutely independent in layout on each other, automatically handle device orientation, and actually limitless in internal content

Cons: Due to used GeometryReader using .infinity at top-level content or pinned view result in crash due to "chicken-egg" problem.

Container code:

struct ContainerWithPinnedBottomView<Content, Pinned>: View 
                                     where Content: View, Pinned: View {

    private var content: () -> Content
    private var bottomView: () -> Pinned

    @inlinable public init(@ViewBuilder pinnedView: @escaping () -> Pinned,
                            @ViewBuilder content: @escaping () -> Content) {
        self.content = content
        self.bottomView = pinnedView
    }

    var body: some View {
        ZStack(alignment: .bottom) {
            Rectangle().fill(Color.clear) // !! Extends ZStack to full screen
            GeometryReader { _ in
                ZStack {
                    self.content()
                }
            }
            self.bottomView()
                .alignmentGuide(.bottom) { $0[.bottom] }
        }
    }

}

Usage example (of demo screenshot)

struct TestBottomView: View {
    var body: some View {
        ContainerWithPinnedBottomView(pinnedView: {
            HStack {
                Spacer()
                Text("Always Pinned to Bottom")
                    .padding()
    //                .frame(width: .infinity) // !! LIMITATION - don't use, cycling crash
                Spacer()
            }
            .background(Color.blue)
        }) {
            NavigationView {
                List (0 ..< 100, id: .self) { i in
                    NavigationLink(destination: Text("Other")) {
                        Text("Row (i)")
                    }
                }
                .navigationBarTitle("TitleBar")
            }
        }
    }
}

struct TestBottomView_Previews: PreviewProvider {
    static var previews: some View {
        TestBottomView()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...