skip to Main Content

It’s fixed at iOS 16.1 Beta 4 ✅


There is a weird crash that only happens in iOS 16. When I try to create a TabView with page style, it’s crashing when the array is empty. But it’s working perfectly fine in iOS 15. Is this an iOS 16 bug?

Xcode version: Version 14.0 (14A309)

iOS Version: 16.0 & 16.1 Beta

struct TestView: View {
    @State private var data: [Int] = []
    var body: some View {
        TabView {
            ForEach(0..<data.count, id: .self) { index in
                RoundedRectangle(cornerRadius: 12)
            }
        }
        .frame(height: 200)
        .tabViewStyle(.page(indexDisplayMode: .never))
        .onAppear {
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                self.data = [1,2,3,4]
            }
        }
    }
}

Crash Log

2022-10-04 18:01:56.275879+0300 TestFirebaseCrash[24331:486169] *** Assertion failure in -[_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView _validateScrollingTargetIndexPath:], UICollectionView.m:7339
2022-10-04 18:01:56.278900+0300 TestFirebaseCrash[24331:486169] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds item (0) when there are only 0 items in section 0. Collection view: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView: 0x140841e00; baseClass = UICollectionView; frame = (0 0; 428 200); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001d8d260>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x600001386d20>; contentOffset: {0, 0}; contentSize: {0, 200}; adjustedContentInset: {0, 0, 0, 0}; layout: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF012PagingLayout: 0x140514090>; dataSource: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF011Coordinator: 0x6000021f46c0>>.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000018040c304 __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x000000018005131c objc_exception_throw + 56
    2   Foundation                          0x0000000180b11f88 _userInfoForFileAndLine + 0
    3   UIKitCore                           0x00000001032279a8 -[UICollectionView _validateScrollingTargetIndexPath:] + 408
    4   UIKitCore                           0x0000000103227cc0 -[UICollectionView _contentOffsetForScrollingToItemAtIndexPath:atScrollPosition:] + 48
    5   UIKitCore                           0x0000000103228838 -[UICollectionView _scrollToItemAtIndexPath:atScrollPosition:animated:] + 220
    6   SwiftUI                             0x0000000106f5de08 OUTLINED_FUNCTION_15 + 18076
    7   SwiftUI                             0x0000000106f5dea8 OUTLINED_FUNCTION_15 + 18236
    8   SwiftUI                             0x0000000106f5dc88 OUTLINED_FUNCTION_15 + 17692
    9   SwiftUI                             0x0000000106f5dcf4 OUTLINED_FUNCTION_15 + 17800
    10  UIKitCore                           0x0000000103ea214c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1920
    11  QuartzCore                          0x0000000187ed749c _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
    12  QuartzCore                          0x0000000187ee1f54 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 128
    13  QuartzCore                          0x0000000187e117d0 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 452
    14  QuartzCore                          0x0000000187e3d47c _ZN2CA11Transaction6commitEv + 652
    15  UIKitCore                           0x0000000103a257f0 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
    16  CoreFoundation                      0x0000000180370e38 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
    17  CoreFoundation                      0x0000000180370580 __CFRunLoopDoBlocks + 364
    18  CoreFoundation                      0x000000018036ae7c __CFRunLoopRun + 776
    19  CoreFoundation                      0x000000018036a75c CFRunLoopRunSpecific + 584
    20  GraphicsServices                    0x0000000188f60c98 GSEventRunModal + 160
    21  UIKitCore                           0x0000000103a0cb74 -[UIApplication _run] + 868
    22  UIKitCore                           0x0000000103a10b1c UIApplicationMain + 124
    23  SwiftUI                             0x0000000107702130 OUTLINED_FUNCTION_51 + 496
    24  SwiftUI                             0x0000000107701fd8 OUTLINED_FUNCTION_51 + 152
    25  SwiftUI                             0x0000000106ee5894 OUTLINED_FUNCTION_10 + 88
    26  TestFirebaseCrash                   0x000000010269ec6c $s17TestFirebaseCrash0abC3AppV5$mainyyFZ + 40
    27  TestFirebaseCrash                   0x000000010269ed14 main + 12
    28  dyld                                0x00000001028e9fa0 start_sim + 20
    29  ???                                 0x00000001029bd08c 0x0 + 4338733196
    30  ???                                 0x1a00000000000000 0x0 + 1873497444986126336
)
libc++abi: terminating with uncaught exception of type NSException
terminating with uncaught exception of type NSException
CoreSimulator 857.10 - Device: iPhone 13 Pro Max (29ECC9E3-B223-4591-A18A-1C858F528C45) - Runtime: iOS 16.0 (20A360) - DeviceType: iPhone 13 Pro Max
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds item (0) when there are only 0 items in section 0. Collection view: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView: 0x140841e00; baseClass = UICollectionView; frame = (0 0; 428 200); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001d8d260>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x600001386d20>; contentOffset: {0, 0}; contentSize: {0, 200}; adjustedContentInset: {0, 0, 0, 0}; layout: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF012PagingLayout: 0x140514090>; dataSource: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF011Coordinator: 0x6000021f46c0>>.'
(lldb) 

2

Answers


  1. Chosen as BEST ANSWER

    It looks like it's fixed at iOS 16.1 Beta 4


  2. Try returning Text("") (basically an empty view) if data is empty.

    TabView {
        if data.isEmpty {
            Text("")
        } else {
            ForEach(data, id: .self) { _ in
                RoundedRectangle(cornerRadius: 12)
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search