Ayush Agrawal
Ayush Agrawal

Reputation: 147

How to show the middle element of array in the collection view when the app is launched?

Can Somebody please tell me, How to show the middle element of array in the collection view when the app is launched and other elements are show in the left and right side of this middle element?

Upvotes: 1

Views: 449

Answers (2)

Jay Patel
Jay Patel

Reputation: 353

In viewDidLayoutSubviews method scroll your collection view to that particular IndexPath. And for do it for first time when launch use bool variable.

var isFirstTime = true
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    if isFirstTime {
        isFirstTime = false
         let selectedIndex = IndexPath(item: 0, section: 0)
          self.collectionView.scrollToItem(at: selectedIndex, at: .centeredHorizontally, animated: false)

    }
}

Upvotes: 1

Dipen Sekhsaria
Dipen Sekhsaria

Reputation: 31

You can use [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(myArr.count/2) inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];

Change the section in index path accordingly.

Upvotes: 0

Related Questions