ios - 快速显示和隐藏导航栏
问题描述
我想创建:显示没有导航栏的视图,当我滚动时,如果从顶部的距离 >= 100 高度到底部显示导航栏。
从底部滚动时:如果到顶部的距离 <= 100 高度需要隐藏导航栏我试试这个,但它没有帮助我
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if(velocity.y>0) {
UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {
self.navigationController?.setNavigationBarHidden(true, animated: true)
}, completion: nil)
} else {
UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {
self.navigationController?.setNavigationBarHidden(false, animated: true)
}, completion: nil)
}
}
解决方案
你需要的功能可以用scrollViewDidScroll
. 我已经实施和测试并且它工作正常。
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("scroll Content : \(scrollView.contentOffset.y)")
if scrollView.contentOffset.y >= 100
{
UIView.animate(withDuration: 2.5, animations: {
self.navigationController?.setNavigationBarHidden(true, animated: true)
})
}
else
{
UIView.animate(withDuration: 2.5, animations: {
self.navigationController?.setNavigationBarHidden(false, animated: true)
})
}
}
在 viewDidLoad() 中,您可以隐藏导航栏,因此当您打开应用程序时,导航栏会被隐藏。
希望这会帮助你。
推荐阅读
- node.js - NodeJS + MQTT - 以 1 毫秒的频率发布会导致 EventEmitter 内存泄漏
- android - 我无法提供带有匕首的视图模型
- python-3.x - 尝试使用 pytest 和 capsys 访问标准输出时出现 ValueError
- visual-studio-code - Visual Studio Code 中的 window.zoomLevel 自动更改为负值
- selenium - Safari 浏览器与 Selenium 有问题
- java - 使用 google chrom 检查时 Stetho SQL 数据库不起作用
- javascript - 如何将具有多个字段的表单发送到服务器,然后将其显示在同一站点的不同选项卡中?
- .htaccess - htaccess:隐藏文件夹并隐藏子文件夹链接中的文件扩展名
- python - Flask restplus swagger "Model
- not registered"
- android - 从冷启动(当应用程序被杀死时)使用 Expo 读取推送通知