Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:34:24+00:00 2026-06-14T07:34:24+00:00

I have a UICollectionView. It scrolls horizontally, has only a single row of items,

  • 0

I have a UICollectionView. It scrolls horizontally, has only a single row of items, and behaves like a paging UIScrollView. I’m making something along the lines of the Safari tab picker, so you can still see the edge of each item. I only have one section.

If I delete an item that is not the last item, everything works as expected and a new item slides in from the right.

If I delete the last item, then the collection view’s scroll position jumps to the N-1th item (doesn’t smoothly animate), and then I see the Nth item (the one I deleted) fade out.

This behaviour isn’t related to the custom layout I made, as it occurs even if I switch it to use a plain flow layout. I’m deleting the items using:

[self.tabCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];

Has anyone else experienced this? Is it a bug in UICollectionView, and is there a workaround?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:34:25+00:00Added an answer on June 14, 2026 at 7:34 am

    I managed to get my implementation working using the standard UICollectionViewFlowLayout. I had to create the animations manually.

    First, I caused the deleted cell to fade out using a basic animation:

    - (void)tappedCloseButtonOnCell:(ScreenCell *)cell {
    
        // We don't want to close our last screen.
        if ([self screenCount] == 1u) {
            return;
        }
    
        [UIView animateWithDuration:UINavigationControllerHideShowBarDuration
                         animations:^{
                             // Fade out the cell.
                             cell.alpha = 0.0f;
                         }
                         completion:^(BOOL finished) {
    
                             NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
                             UIViewController *screen = [self viewControllerAtIndex:indexPath.item];
    
                             [self removeScreen:screen animated:YES];
                         }];
    }
    

    Next, I caused the collection view to scroll to the previous cell. Once I’ve scrolled to the desired cell, I remove the deleted cell.

    - (void)removeScreen:(UIViewController *)screen animated:(BOOL)animated {
    
        NSParameterAssert(screen);
    
        NSInteger index = [[self.viewControllerDictionaries valueForKeyPath:kViewControllerKey] indexOfObject:screen];
    
        if (index == NSNotFound) {
            return;
        }
    
        [screen willMoveToParentViewController:nil];
    
        if (animated) {
    
            dispatch_time_t popTime = DISPATCH_TIME_NOW;
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index
                                                     inSection:0];
    
            // Disables user interaction to make sure the user can't interact with
            // the collection view during the time between when the scroll animation
            // ends and the deleted cell is removed.
            [self.collectionView setUserInteractionEnabled:NO];
    
            // Scrolls to the previous item, if one exists. If we are at the first
            // item, we just let the next screen slide in from the right.
            if (index > 0) {
                popTime = dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC);
                NSIndexPath *targetIndexPath = [NSIndexPath indexPathForItem:index - 1
                                                      inSection:0];
                [self.collectionView scrollToItemAtIndexPath:targetIndexPath
                                            atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                                    animated:YES];
            }
    
            // Uses dispatch_after since -scrollToItemAtIndexPath:atScrollPosition:animated:
            // doesn't have a completion block.
            dispatch_after(popTime, dispatch_get_main_queue(), ^{
    
                [self.collectionView performBatchUpdates:^{
                    [self.viewControllerDictionaries removeObjectAtIndex:index];
                    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
                    [screen removeFromParentViewController];
                    [self.collectionView setUserInteractionEnabled:YES];
                } completion:NULL];
            });
    
        } else {
            [self.viewControllerDictionaries removeObjectAtIndex:index];
            [self.collectionView reloadData];
            [screen removeFromParentViewController];
        }
    
        self.addPageButton.enabled = YES;
        [self postScreenChangeNotification];
    }
    

    The only part that is slightly questionable is the dispatch_after(). Unfortunately, -scrollToItemAtIndexPath:atScrollPosition:animated: does not have a completion block, so I had to simulate it. To avoid timing problems, I disabled user interaction. This prevents the user from interacting with the collection view before the cell is removed.

    Another thing I had to watch for is I have to reset my cell’s alpha back to 1 due to cell reuse.

    I hope this helps you with your Safari-style tab picker. I know your implementation is different from mine, and I hope that my solution works for you too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UICollectionView showing several items. I also have an edit button in
I have a UICollectionView which I am trying to insert items into it dynamically/with
I recently started playing with the awesome UICollectionView API, making reasonable progress, but have
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I'm making a horizontal picker by using a UICollectionView . It's simple enough: A
I have a UICollectionView which uses UICollectionViewDelegateFlowLayout . I've added a method which changes
I have a very simple list of objects that represt people. Each object has
I have a view set up with two UICollectionViews. Each of these views has
I have a UICollectionView , and it works great, but I have a doubt.
I have a UICollectionViewController : - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.pageTastes count]; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.