如何在“集合”视图的elementKindSectionHeader下附加UI视图?

时间:2019-02-19 22:10:56

标签: ios swift uicollectionview

我创建了一个集合视图,并通过注册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)
    }

这给了我一个不错的标题,例如: enter image description here

但是现在我要在标题的下方下,但在集合视图的主要部分上方中使用菜单栏。我创建了具有正确尺寸的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,并且如果您向上或向下滚动都不会移动

enter image description here

有人对此有想法吗?

1 个答案:

答案 0 :(得分:1)

如果您希望此菜单栏随着UITableView滚动移动,则应将其放置在标题内的“主标题”下方。

这种方法抽象是将标题+菜单栏视为一个标题。

相关问题