有没有一种方法可以初始化带有内容偏移量的UICollectionView?

时间:2020-10-30 00:11:31

标签: swift uicollectionview

我想知道是否有任何方法可以使用内容偏移量来初始化自定义UICollectionView,以便在其首次出现时,集合视图是偏移量?我尝试在自定义UICollectionView的conventioninit中设置内容偏移量,但是当集合视图首次出现时,这不会影响内容偏移量。谢谢

我的自定义类的便捷初始化程序:

convenience init() {
  layout = UICollectionViewFlowLayout()
  layout.scrollDirection = .horizontal
  layout.minimumLineSpacing = 0
  self.init(frame: CGRect.zero, collectionViewLayout: layout)
  self.backgroundColor = UIHelper.primaryBG
  self.showsHorizontalScrollIndicator = false

  contentInset = UIEdgeInsets(top: 0,
                              left: UIScreen.main.bounds.width / 2,
                              bottom: 0,
                              right: UIScreen.main.bounds.width / 2)

  // Doesn't work
  setContentOffset(CGPoint(x: UIScreen.main.bounds.width * 1.5, y: 0), animated: false) 

  self.register(MyCell.self, forCellWithReuseIdentifier: "cell")
  self.delegate = self
  self.dataSource = self
}

1 个答案:

答案 0 :(得分:1)

尝试将setContentOffset移出init并在数据实际位于collectionView之后调用它,例如在调用reloadData之后