BLACK FRIDAY: Save 50% on all my books and bundles! >>

How to disable ScrollView clipping so contents overflow

Paul Hudson    @twostraws   

Updated for Xcode 16.4

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 project

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.
Save 50% in my Black Friday sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.

Save 50% on all our books and bundles!

Similar solutions…

BUY OUR BOOKS
Buy Everything but the Code Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Interview Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account