9

I have a shared view in my app that can be called in different places. The root of this view is a scroll view, but sometimes it ignores the top safe area and it collapses under the navigation bar.

Here two screenshots that shows better the problem:

enter image description here enter image description here

As you can see in the second screenshot the scrollview extends for all the screen collapsing under the navigation. How can I avoid this?

Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70

6 Answers6

27

Adding .padding(.top, 1) to the scroll view will solve the issue.

Ihor Vovk
  • 551
  • 6
  • 11
  • Not a perfect solution but it does solve the issue. How would you do it so the navigation header gets pinned during scroll? – Arunabh Das Jan 30 '22 at 21:22
6

I had a similar issue with the List inside NavigationView and ignored the bottom safe area. What helped me was setting the list bottom padding to some non zero value, like this:

.padding(.bottom, 1)

It looks like a bug and this issue still exists on iOS 15.2

5

Ihor Vovk's solution works perfectly, but if you don't want a visible padding, just change it to .padding(.top, 0.1). (I tested and 0.1 is the smallest possible value that makes it work)

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 0.1 doesn't work for me. 0.2 does work however, but it's noticeable. 0.15 works too, and while still noticeable it is much more discreet than 0.2. It might vary between devices though. I have only tested on one. – KPM Feb 23 '23 at 22:51
1

Solved using:

.navigationViewStyle(StackNavigationViewStyle())

in all the NavigationView

Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70
1

Think in a simpler way, you will be surprised please use modifier .clipped() -> The problem is that the scroll view is allowing its content to overflow, like the masksToBounds property in UIkit, the padding(.top, 1) solution above is also a way to prevent the content from overflowing.

0

You can solved it by giving padding to scroll view

ScrollView {
    VStack(spacing: 0) {
      
        Text("test")
        Text("test")
        Text("test")
        Text("test")
        Text("test")
        
    }
}
.padding(.top, UIApplication.shared.windows.first!.safeAreaInsets.top )
Arvind Patel
  • 441
  • 6
  • 13