0

I have a straightforward ScrollView that shows paging behavior like this:

import SwiftUI

struct ScrollTest: View {
   var body: some View {
     ScrollView {
        VStack{
            ForEach(0..<10, id: \.self) { index in
                Rectangle()
                    .ignoresSafeArea()
                    .frame(maxWidth:.infinity)
                    .containerRelativeFrame(.vertical, alignment:.center)
            }
        }
        .scrollTargetLayout()
     }
     .scrollTargetBehavior(.paging)
 }
}

The issue is that when launched on a simulator (or real device), the app crashes with a console error (it works just fine in the preview):

"Invalid frame dimension (negative or non-finite). *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 nan; 388 inf]. Layer: <CALayer:0x600000258fa0; position = CGPoint (194 inf); bounds = CGRect (0 0; 388 inf); delegate = <SwiftUI.HostingScrollView: 0x10282c200; baseClass = UIScrollView; frame = (0 nan; 388 inf); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000c8bdb0>; layer = <CALayer: 0x600000258fa0>; contentOffset: {0, 0}; contentSize: {388, 70.333333333333329}; adjustedContentInset: {nan, 0, 0, 0}>; sublayers = (<CALayer: 0x600000258280>, <CALayer: 0x6000002a1b40>); opaque = YES; masksToBounds = YES; allowsGroupOpacity = YES; >'"

The problem disappears when I remove the ".containerRelativeFrame(.vertical, alignment:.center)" modifier, but the scrolling behavior I desire (TikTok-style scrolling) is not achieved.

Has anyone encountered a similar problem? Thank you.

4
  • What OS version are you running this? I cannot reproduce this on iOS 18.1 simulator. Commented Mar 4 at 14:32
  • Cannot reproduce either, but in case you haven't noticed it, there's also the issue of the ever increasing bottom area as you scroll through. You may want to look at this answer for more info and solutions. Commented Mar 4 at 19:21
  • Cannot reproduce either, works well for me on macOS 15.4, using Xcode 16.3-beta, target iOS-18.4, tested on real iOS device, simulators, MacCatalyst and MacOS. It could be different on older systems. Note, containerRelativeFrame(_:alignment:_:) is for iOS17+ Commented Mar 5 at 0:57
  • Thank you, guys. I'm targeting iOS 18.2 on the simulator with Xcode 16.2 running on macOS 15.3.1 Commented Mar 5 at 7:10

1 Answer 1

0

After going through a long process of trial and error, I discovered that the issue stemmed from having a ScrollView nested inside another ScrollView. Removing the parent ScrollView fixed the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.