我有两个单独的视图,每个视图中都有一个collectionView
,两个视图都有相同的顶部和按钮约束锚
宽度与屏幕边界的大小相同
private lazy var screenSize = UIScreen.main.bounds.width
这是限制条件
friendsCollectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
friendsCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
friendsCollectionView.widthAnchor.constraint(equalToConstant: screenSize).isActive = true
friendsCollectionView.leadingAnchor.constraint(equalTo: discoveryCollectionView.trailingAnchor).isActive = true
friendsViewTrillingAnchor = friendsCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0)
friendsViewTrillingAnchor.isActive = true
discoveryCollectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
discoveryCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
discoveryCollectionView.widthAnchor.constraint(equalToConstant: screenSize).isActive = true
discoveryViewLeadingAnchor = discoveryCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: -screenSize)
discoveryViewLeadingAnchor.isActive = true
并且有分段控制来激活他们中的每一个。我做了一个动画,他们移动到左边和右边的屏幕,为另一个腾出空间-见截图
当我按下每个分段按钮时,我调用handleFeedsTransition
函数
private func handleFeedsTransition(with feedsType: FeedsType ) {
switch feedsType {
case .discovery:
self.feedsType = .discovery
handleFeedsTransitionAnimation()
case .friends:
self.feedsType = .friends
handleFeedsTransitionAnimation()
}
}
private func handleFeedsTransitionAnimation() {
switch feedsType {
case .discovery:
UIView.animate(withDuration: 0.4, animations: { [weak self] in
guard let this = self else { return }
this.discoveryCollectionView.isHidden = false
this.discoveryViewLeadingAnchor.constant = 0
this.friendsViewTrillingAnchor.constant = this.screenSize
this.view.layoutIfNeeded()
}, completion: { [weak self] _ in
guard let this = self else { return }
this.friendsCollectionView.isHidden = true
})
case .friends:
UIView.animate(withDuration: 0.4, animations: { [weak self] in
guard let this = self else { return }
this.friendsCollectionView.isHidden = false
this.discoveryViewLeadingAnchor.constant = -this.screenSize
this.friendsViewTrillingAnchor.constant = 0
this.view.layoutIfNeeded()
}, completion: { [weak self] _ in
guard let this = self else { return }
this.discoveryCollectionView.isHidden = true
})
}
}
这是导航栏配置
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
navigationController?.navigationBar.prefersLargeTitles = true
因此,有一个简单的问题,我希望两个视图在滚动时都使导航栏变小,但当我在view.subView
中添加它们时,第一个视图总是这样做,而第二个视图不会使导航栏变小
这里
view.addSubview(todoCollectionView)
view.addSubview(taskCollectionView)
只有todoCollectionView
执行此操作,如果我更改它们的顺序,则只有taskCollectionView
执行此操作
有办法修好吗?非常感谢
1条答案
按热度按时间rqmkfv5c1#
注意-我们可能知道,为了使用自动大标题展开/折叠显示,第一个子视图必须是滚动视图(
UICollectionView
、UITableView
、UIScrollView
等)。所以,我想......要从“发现”到“朋友”,让我们将z顺序交换为:
不幸的是,这并不起作用。显然,导航栏的功能是在添加视图时确定的。因此,所做的一切就是从两个集合视图中删除该功能:(
对于解决此问题的一种方法,我们可以:
.removeFromSuperview()
删除两个集合视图view.insertSubview(friendsCollectionView, at: 0)
view.insertSubview(discoveryCollectionView, at: 1)
这里有一个简单的例子...
首先,简单的单标签单元格:
以及示例视图控制器: