ios - How to make custom label as UINavBar disappear when scrolling down a table view?
问题描述
I have recently implemented a custom UINavigationBar in my app that uses a label instead of the normal largeTitle. I have been trying to make this disappear when the user scrolls down the table view.
So far I have tried to set label.isHidden = true when the third cell is displayed as that is the next cell not on the original page and so indicates a scroll. However this means that the label does not disappear until a certain amount has been scrolled.
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath == 2 {
label.isHidden = true
}
}
Instead, I would like this custom navBar label to be hidden as soon as scrolling begins and to reappear when scrolling comes back to the top just like the default UINavigationBar does.
Thanks.
解决方案
您可以尝试以下代码:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if(velocity.y > 0) {
self.navLabel.isHidden = true
} else {
self.navLabel.isHidden = false
}
}
或者,如果您想要与 UINavigationBar Hide / Show 相同的动画,请使用以下代码:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if(velocity.y > 0) {
UIView.animate(withDuration: 0.5, delay: 0, options: UIView.AnimationOptions(), animations: {
let labelFrame = self.navLabel.frame
self.navLabel.frame = CGRect(x: labelFrame.origin.x, y: -64, width: labelFrame.size.width, height: labelFrame.size.height)
}, completion: nil)
} else {
UIView.animate(withDuration: 0.5, delay: 0, options: UIView.AnimationOptions(), animations: {
let labelFrame = self.navLabel.frame
self.navLabel.frame = CGRect(x: labelFrame.origin.x, y: 0, width: labelFrame.size.width, height: labelFrame.size.height)
}, completion: nil)
}
}
推荐阅读
- c++ - 如何在不同的行上返回大写和小写字符串?
- react-leaflet - React-leaflet 集群在反应钩子中不起作用
- spring - 使用 Junit 和 Mockito 嵌套异常问题测试 POST Api
- python - Scrapy & Selenium:如何在循环中调用方法
- design-patterns - 如何使用可更新缓存实现存储库模式
- angular - 带有辅助路线的 Angular 6 材质选项卡导航
- mongodb - 如何在开始测试之前删除 mongodb 中的所有模型并注册用户?
- java - 什么是@constructorproperties?这是属于 J2SE 还是 Spring?
- java - HTTP 500 内部服务器错误:在容器化我的 Java Web 应用程序时
- templates - 更改 svelte 模板的默认位置