问题描述
我在父 UIView 中创建了一个水平滚动集合视图,并将其添加到我的 UIViewController 中。但是当我滚动它时滚动视图会向上移动。
这是容器视图的初始化方式:
override init(frame: CGRect){
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = UICollectionViewFlowLayoutAutomaticSize
layout.estimatedItemSize = CGSize(width: 100,height: 50)
countryCollectionView = UICollectionView(frame: CGRect.zero,collectionViewLayout: layout)
viewTitleLabel = UILabel()
super.init(frame: frame)
setupTableView()
sortedDummyData = dummyData.sorted(by: <)
}
对我的子视图的限制:
NSLayoutConstraint.activate([
viewTitleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor,constant: 5),viewTitleLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor),viewTitleLabel.heightAnchor.constraint(equalToConstant: 30),viewTitleLabel.widthAnchor.constraint(equalToConstant: 90),countryCollectionView.leadingAnchor.constraint(equalTo: viewTitleLabel.trailingAnchor,constant: 10),countryCollectionView.centerYAnchor.constraint(equalTo: self.centerYAnchor),countryCollectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor,constant: -10),countryCollectionView.heightAnchor.constraint(equalToConstant: 50)
])
添加到 UIViewContoller 时对容器视图的约束:
countryCountView.translatesAutoresizingMaskIntoConstraints = false
countryCountView.isUserInteractionEnabled = true
view.addSubview(countryCountView)
NSLayoutConstraint.activate([
countryCountView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 0),countryCountView.trailingAnchor.constraint(equalTo: view.trailingAnchor),countryCountView.topAnchor.constraint(equalTo: headerContainer.bottomAnchor),countryCountView.heightAnchor.constraint(equalToConstant: 60)
])
我的 UICollectionViewCell:
override init(frame: CGRect) {
countryNameLabel = UILabel()
countryUserCountLabel = UILabel()
super.init(frame: frame)
contentView.addSubview(countryNameLabel)
countryNameLabel.translatesAutoresizingMaskIntoConstraints = false
countryNameLabel.font = UIFont.boldSystemFont(ofSize: 13)
countryNameLabel.textColor = .white
contentView.addSubview(countryUserCountLabel)
countryUserCountLabel.translatesAutoresizingMaskIntoConstraints = false
countryUserCountLabel.font = UIFont.systemFont(ofSize: 13)
countryUserCountLabel.textColor = .white
NSLayoutConstraint.activate([
countryNameLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor,countryNameLabel.heightAnchor.constraint(equalToConstant: 30),countryNameLabel.topAnchor.constraint(equalTo: self.topAnchor,countryNameLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: -5),// countryNameLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 0),// countryNameLabel.widthAnchor.constraint(equalToConstant: 20),countryUserCountLabel.leadingAnchor.constraint(equalTo: countryNameLabel.trailingAnchor,countryUserCountLabel.topAnchor.constraint(equalTo: self.topAnchor,countryUserCountLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor,countryUserCountLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor,countryUserCountLabel.heightAnchor.constraint(equalToConstant: 30),// countryUserCountLabel.widthAnchor.constraint(equalToConstant: 40)
])
}
委托方法返回的高度:
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 70,height: 40)
// return CGSize.zero
}
滚动前:
滚动后:
编辑:
设置 collectionView.showsHorizontalScrollIndicator = false
没有帮助。
还设置 layout.estimatedItemSize = CGSize.zero
修复了向上移动但阻止单元格正确调整大小。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)