Grid layout with CollectionView in Swift

Create the UICollectionViewController like this in a file that sub-classes from UICollectionViewController: convenience override init() { var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.itemSize = CGSizeMake(<width>, <height>) // Setting the space between cells layout.minimumInteritemSpacing = <Space between columns> layout.minimumLineSpacing = <Space between rows> return (self.init(collectionViewLayout: layout)) } In the viewDidLoad you an set the background color like … Read more

What’s causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist

The crash was happening in the following situation: We had a collection view controller that was presenting another view controller on top of it. While the collection view controller was no longer visible, the following sequence of events was occasionally occurring in response to our app’s back end requests. [UICollectionView insertItemsAtIndexPaths:] was called with 50 … Read more

UICollectionView automatically scroll to bottom when screen loads

I found that nothing would work in viewWillAppear. I can only get it to work in viewDidLayoutSubviews: – (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:endOfModel inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO]; }

Where to highlight UICollectionViewCell: delegate or cell?

As the documentation says, you can rely on highlighted property to be changed while the cell is highlighted. For example the following code will make the cell red when highlighted (not its subviews though): – (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; [self setNeedsDisplay]; } – (void)drawRect:(CGRect)rect { [super drawRect:rect]; if (self.highlighted) { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, … Read more

UICollectionView is scrolling when selecting a cell

If you are selecting the cell programmatically with collectionview.selectItem(at: indexpath, animated: true, scrollPosition: .top) — and since you didn’t share any of your code with us, let’s assume that’s correct… … then like me you might not have realized that you can use an empty set like this: collectionview.selectItem(at: indexpath, animated: true, scrollPosition: [])

Animation Duration for UICollectionView selectItemAtIndexPath:animated:scrollPosition:

I had similar problems with UITableView which I solved by the following code: [CATransaction begin]; [CATransaction setCompletionBlock:onCompletion]; [CATransaction setAnimationDuration:duration]; [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:3] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; [CATransaction commit]; Obviously, you can’t call it with animated:NO because the animation code inside the method is important. Using a CATransaction to wrap the animation worked for me.

UICollectionView container padding

Please try to put following code inside viewDidLoad(): collectionView!.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) This will add padding to every side of the collectionView. PS: I know the question is here for a while but it might help somebody 😉

UICollectionView – didDeselectItemAtIndexPath not called if cell is selected

The issue is, that the cell is selected, but the UICollectionView does not know anything about it. An extra call for the UICollectionView solves the problem: [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; Full code: – (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@”Cell” forIndexPath:indexPath]; cell.selected = YES; [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; return cell; } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)