Swift 3.0或更高版本
在实现scrollToItem方法之前添加"view.layoutIfNeeded()",理想情况下在viewWillAppear中
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) view.layoutIfNeeded() colView.scrollToItem(at: IndexPath(item: 4, section: 0), at: .centeredHorizontally, animated: true)}
我使用dispatch_async来更新UI并且它可以工作.
let numberOfSections = self.collectionView.numberOfSections() let numberOfRows = self.collectionView.numberOfItemsInSection(numberOfSections-1) if numberOfRows > 0 { dispatch_async(dispatch_get_main_queue(), { let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1)) self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true) }) }