Home > Software design >  How to prevent covering a ScrollView embedded in ZStack when scrolling to the bottom of the ScrollVi
How to prevent covering a ScrollView embedded in ZStack when scrolling to the bottom of the ScrollVi

Time:12-30

I have HStack and ScrollView in ZStack, how can I prevent the HStack from covering the bottom of ScrollView? I think there is a modifier to fix this issue, but I can’t remember it.

struct Messages: View {
    var body: some View {
        ZStack(alignment: .bottom) {
            ScrollView {
                VStack() {
                    MessagesList()
                }
            }
            MessageInput()
        }
    }
}

CodePudding user response:

You can drop the ZStack and declare the view that sticks to the bottom using the .safeAreaInset modifier, e.g.:

ScrollView {
  // etc.
}
.safeAreaInset(edge: .bottom) {
  MessageInput()
}

More information on Hacking with Swift and Apple’s developer docs

  • Related