Skip to main content

How to disable ScrollView clipping so contents overflow

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to disable ScrollView clipping so contents overflow 관련

SwiftUI by Example

Back to Home

How to disable ScrollView clipping so contents overflow | SwiftUI by Example

How to disable ScrollView clipping so contents overflow

Updated for Xcode 15

New in iOS 17

SwiftUI's ScrollView automatically clips its contents, so that scrolling views always stay fully inside the scroll view area. However, if you use the scrollClipDisabled() modifier you can override this default behavior, allowing scrolling views to overflow.

Important

This does not affect the touch area of the scroll view, so if users tap on views that have overflowed your ScrollView that tap will actually be received by whatever is below. So, it's probably best to restrict this a little, for example to allow shadows to flow outside the scrolling area without affecting other views too much.

As an example, this next example shows a VStack with fixed text at the top and bottom, with a scrolling area in the middle. The scrolling views will start neatly aligned below the top text, but as you scroll will overflow:

VStack {
    Text("Fixed at the top")
        .frame(maxWidth: .infinity)
        .frame(height: 100)
        .background(.green)
        .foregroundStyle(.white)

    ScrollView {
        ForEach(0..<5) { i in
            Text("Scrolling")
                .frame(maxWidth: .infinity)
                .frame(height: 200)
                .background(.blue)
                .foregroundStyle(.white)
        }
    }
    .scrollClipDisabled()

    Text("Fixed at the bottom")
        .frame(maxWidth: .infinity)
        .frame(height: 100)
        .background(.green)
        .foregroundStyle(.white)
}

Download this as an Xcode projectopen in new window

Views inside a scrollview scrolling outside their container as the user moves around.
Views inside a scrollview scrolling outside their container as the user moves around.

There are two extra things it's helpful to know when working with scrollClipDisabled():

  1. You can add a custom clip shape to limit how far things overflow. For example, adding padding() then clipShape(.rect) means you get a little overflow, but not infinite.
  2. Because scrolling views now overlap their surroundings, you may need to use zIndex() to adjust their vertical positioning. For example, if other views have the default Z index, then using zIndex(1) on your scroll view will make its children render over other views.
Similar solutions…
How to disable autocorrect in a TextField | SwiftUI by Example

How to disable autocorrect in a TextField
How to disable taps for a view using allowsHitTesting() | SwiftUI by Example

How to disable taps for a view using allowsHitTesting()
How to disable the overlay color for images inside Button and NavigationLink | SwiftUI by Example

How to disable the overlay color for images inside Button and NavigationLink
How to indent the content or scroll indicators in a ScrollView | SwiftUI by Example

How to indent the content or scroll indicators in a ScrollView
How to flash the scroll bar indicators of a ScrollView or List | SwiftUI by Example

How to flash the scroll bar indicators of a ScrollView or List

이찬희 (MarkiiimarK)
Never Stop Learning.