有没有办法用内容偏移量初始化 UICollectionView?
Is there a way to initialize a UICollectionView with a content offset?
我想知道是否有任何方法可以使用内容偏移来初始化自定义 UICollectionView
,以便在它首次出现时集合视图发生偏移?我尝试在自定义 UICollectionView
的 convenienceinit 中设置内容偏移量,但这并不影响集合视图首次出现时的内容偏移量。谢谢
我自定义的便捷初始化器class:
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
}
尝试将 setContentOffset
移出 init 并在数据实际位于 collectionView 之后调用它,例如在调用 reloadData
之后
我想知道是否有任何方法可以使用内容偏移来初始化自定义 UICollectionView
,以便在它首次出现时集合视图发生偏移?我尝试在自定义 UICollectionView
的 convenienceinit 中设置内容偏移量,但这并不影响集合视图首次出现时的内容偏移量。谢谢
我自定义的便捷初始化器class:
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
}
尝试将 setContentOffset
移出 init 并在数据实际位于 collectionView 之后调用它,例如在调用 reloadData