最佳答案
首先添加 header view 作为 UIvVew 并在 headerView 下方添加 UIScrollView 或 UITableView 与屏幕截图相同,然后按照以下步骤操作。
为标题 View 设置固定高度约束(例如 125)并将其附加到顶部、左侧和右侧。
让下面的 UIScrollView 使用所有可用空间,因此将顶部、底部、左侧和右侧约束设置为零。
将页眉 View 高度约束连接到 ViewController 以实现类似以下内容:
@IBOutlet var headerViewHeightConstraint: NSLayoutConstraint!
将 UIScrollView 委托(delegate)设置为 ViewController
声明两个属性来限制标题 View 的最大高度和最小高度,fox 示例:
let headerViewMaxHeight: CGFloat = 125 let headerViewMinHeight: CGFloat = 44 + UIApplication.shared.statusBarFrame.height
整个解决方法基于在 UIScrollView 滚动时更新标题 View 高度约束,因此让我们实现 UIScrollViewDelegate 和我们案例中最重要的委托(delegate),scrollViewDidScroll:
func scrollViewDidScroll(_ scrollView: UIScrollView) { let headerViewMinHeight: CGFloat = 44 + UIApplication.shared.statusBarFrame.height let yPos = mainScrollView.contentOffset.y let newHeaderViewHeight: CGFloat = headerViewHeightConstraint.constant - yPos if newHeaderViewHeight > headerViewMaxHeight { // Here, Manage Your Score Format View headerViewHeightConstraint.constant = max(headerViewMaxHeight, newHeaderViewHeight) } else if newHeaderViewHeight < headerViewMinHeight { headerViewHeightConstraint.constant = headerViewMinHeight } else { headerViewHeightConstraint.constant = newHeaderViewHeight scrollView.contentOffset.y = 0 // block scroll view } }
关于ios - 快速滚动时自定义标题内容动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56371946/