我创建了一个Collection View,并通过注册Header类充分利用了UICollectionView.elementKindSectionHeader
。请参阅下面的代码:
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionViewLayout()
setupCollectionView()
setupMenuBar()
}
var headerView: HeaderView?
fileprivate func setupCollectionViewLayout() {
if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
layout.sectionInset = .init(top: padding, left: padding, bottom: padding, right: padding)
}
}
fileprivate func setupCollectionView() {
self.collectionView.backgroundColor = .white
//For iPhone X's make sure it doesn't cover the swipe bar at bottom
self.collectionView.contentInsetAdjustmentBehavior = .never
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
self.collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
self.collectionView.contentInset = UIEdgeInsets(top: 60, left: 0, bottom: 0, right: 0)
self.collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 60, left: 0, bottom: 0, right: 0)
}
但是现在我想在标题下面有一个菜单栏,但是在集合视图的主要部分上方。我创建了一个具有正确尺寸的MenuBar,但我想不出要约束它的是什么?请参阅下面的代码,目前添加菜单栏:
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
fileprivate func setupMenuBar() {
view.addSubview(menuBar)
view.addConstraintWithFormat(format: "H:|[v0]|", views: menuBar)
view.addConstraintWithFormat(format: "V:|-400-[v0(50)]|", views: menuBar)
}
..但这实际上只是将整个视图中的条形400px固定,如果向上或向下滚动则不会移动
有没有人对此有所了解?
如果您希望此菜单栏随UITableView
滚动移动,则应将其放置在“主标题”下方的标题内。
这种方法抽象是将标题+菜单栏视为一个标题。