在 swift 4 中滚动时隐藏标签栏
Hide tab bar when tableview scroll in swift 4
当我滚动 tableview 时,我有带 tableview 的视图控制器,我想在视图控制器中隐藏选项卡栏。我试过下面的代码它的工作但顶部标签减去原点位置 Y
extension UITabBarController {
func setTabBarVisible(visible:Bool, duration: TimeInterval, animated:Bool) {
if (tabBarIsVisible() == visible) { return }
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = (visible ? -height : height)
// animation
if #available(iOS 10.0, *) {
UIViewPropertyAnimator(duration: duration, curve: .linear) {
self.tabBar.frame.offsetBy(dx:0, dy:offsetY)
self.view.frame = CGRect(x:0,y:0,width: self.view.frame.width, height: self.view.frame.height + offsetY)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}.startAnimation()
} else {
// Fallback on earlier versions
}
}
func tabBarIsVisible() ->Bool {
return self.tabBar.frame.origin.y < UIScreen.main.bounds.height
}
}
//Scroll view delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
//scrolling down
self.tabBarController?.setTabBarVisible(visible: false, duration: 0.3, animated: true)
}
else{
//scrolling up
self.tabBarController?.setTabBarVisible(visible: true, duration: 0.3, animated: true)
}
}
这是它的视频link
https://drive.google.com/file/d/1yNc_mYNySFEd5XLxYn6PG4uizBu82hIp/view
谢谢
您的 Tabbar 框架设置不正确。修改您的代码如下。有效
//Scroll view delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
//scrolling down
self.tabBarController?.setTabBarVisible(visible: false, duration: 0.3, animated: true, tableView: tableView)
}
else{
//scrolling up
self.tabBarController?.setTabBarVisible(visible: true, duration: 0.3, animated: true, tableView: tableView)
}
}
func setTabBarVisible(visible:Bool, duration: TimeInterval, animated:Bool,tableView:UITableView) {
if (tabBarIsVisible() == visible) { return }
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = (visible ? -height : height)
let heightToAdjusted = visible ? 0:22
// animation
if #available(iOS 10.0, *) {
UIViewPropertyAnimator(duration: duration, curve: .linear) {
self.tabBar.frame = CGRect(x: 0, y: self.view.frame.height+offsetY, width: self.view.frame.width, height: height)
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.bounds.width, height: tableView.bounds.height+offsetY)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}.startAnimation()
} else {
// Fallback on earlier versions
}
}
当我滚动 tableview 时,我有带 tableview 的视图控制器,我想在视图控制器中隐藏选项卡栏。我试过下面的代码它的工作但顶部标签减去原点位置 Y
extension UITabBarController {
func setTabBarVisible(visible:Bool, duration: TimeInterval, animated:Bool) {
if (tabBarIsVisible() == visible) { return }
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = (visible ? -height : height)
// animation
if #available(iOS 10.0, *) {
UIViewPropertyAnimator(duration: duration, curve: .linear) {
self.tabBar.frame.offsetBy(dx:0, dy:offsetY)
self.view.frame = CGRect(x:0,y:0,width: self.view.frame.width, height: self.view.frame.height + offsetY)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}.startAnimation()
} else {
// Fallback on earlier versions
}
}
func tabBarIsVisible() ->Bool {
return self.tabBar.frame.origin.y < UIScreen.main.bounds.height
}
}
//Scroll view delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
//scrolling down
self.tabBarController?.setTabBarVisible(visible: false, duration: 0.3, animated: true)
}
else{
//scrolling up
self.tabBarController?.setTabBarVisible(visible: true, duration: 0.3, animated: true)
}
}
这是它的视频link https://drive.google.com/file/d/1yNc_mYNySFEd5XLxYn6PG4uizBu82hIp/view
谢谢
您的 Tabbar 框架设置不正确。修改您的代码如下。有效
//Scroll view delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
//scrolling down
self.tabBarController?.setTabBarVisible(visible: false, duration: 0.3, animated: true, tableView: tableView)
}
else{
//scrolling up
self.tabBarController?.setTabBarVisible(visible: true, duration: 0.3, animated: true, tableView: tableView)
}
}
func setTabBarVisible(visible:Bool, duration: TimeInterval, animated:Bool,tableView:UITableView) {
if (tabBarIsVisible() == visible) { return }
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = (visible ? -height : height)
let heightToAdjusted = visible ? 0:22
// animation
if #available(iOS 10.0, *) {
UIViewPropertyAnimator(duration: duration, curve: .linear) {
self.tabBar.frame = CGRect(x: 0, y: self.view.frame.height+offsetY, width: self.view.frame.width, height: height)
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.bounds.width, height: tableView.bounds.height+offsetY)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}.startAnimation()
} else {
// Fallback on earlier versions
}
}