我的VC有一个tabBarController,当我在tableView中滚动时,我想要根据滚动来隐藏/显示。
我实现了以下方法:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
//scrolling down
changeTabBar(hidden: true, animated: true)
}
else{
//scrolling up
changeTabBar(hidden: false, animated: true)
}
}
func changeTabBar(hidden:Bool, animated: Bool){
let tabBar = self.tabBarController?.tabBar
let offset = (hidden ? UIScreen.main.bounds.size.height : UIScreen.main.bounds.size.height - (tabBar?.frame.size.height)! )
if offset == tabBar?.frame.origin.y {return}
self.mainCollectionView.frame.size.height = self.mainCollectionView.frame.size.height + (tabBarController?.tabBar.frame.size.height)!
let duration:TimeInterval = (animated ? 0.5 : 0.0)
UIView.animate(withDuration: duration,
animations: {tabBar!.frame.origin.y = offset},
completion:nil)
}
但我认为这与self.mainCollectionView.frame.size.height = self.mainCollectionView.frame.size.height + (tabBarController?.tabBar.frame.size.height)!
有关。在那个函数中调用它。我不确定。有时它确实起作用,有时它是一个黑色的tabBar,它也弄乱了我的滚动。有时我不能再滚动了..但是如果我评论它,它工作得很好
发布于 2018-05-08 11:38:04
我已经创建了以下方法,并且在每种情况下都工作得很好
在XCODE中测试编辑
将其放入UITabbarController的子类中
var isTabBarHidden:Bool = false
func setTabBarHidden(_ tabBarHidden: Bool, animated: Bool,completion:(() -> Void)? = nil) {
if tabBarHidden == isTabBarHidden {
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
//check tab bar is visible and view and window height is same then it should be 49 + window Heigth
if (tabBarHidden == true && UIScreen.main.bounds.height == self.view.frame.height) {
let offset = self.tabBar.frame.size.height
self.view.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height + offset)
}
if let block = completion {
block()
}
return
}
isTabBarHidden = tabBarHidden
let offset: CGFloat? = tabBarHidden ? self.tabBar.frame.size.height : -self.tabBar.frame.size.height
UIView.animate(withDuration: animated ? 0.250 : 0.0, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.curveEaseIn, .layoutSubviews], animations: {() -> Void in
self.tabBar.center = CGPoint(x: CGFloat(self.tabBar.center.x), y: CGFloat(self.tabBar.center.y + offset!))
//Check if View is already at bottom so we don't want to move view more up (it will show black screen on bottom ) Scnario : When present mail app
if (Int(offset!) <= 0 && UIScreen.main.bounds.height == self.view.frame.height) == false {
self.view.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height + offset!)
}
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}, completion: { _ in
if let block = completion {
block()
}
})
}
从你的ViewController
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
//scrolling down
(self.tabBarController as! YourTabbarControllerClassName).setTabBarHidden(true, animated: true)
}
else{
//scrolling up
(self.tabBarController as! YourTabbarControllerClassName).setTabBarHidden(false, animated: true)
}
}
希望能对大家有所帮助
发布于 2019-08-14 15:14:48
在你的prepare for segue
中设置hidesBottomBarWhenPushed
it
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination.hidesBottomBarWhenPushed = true
}
发布于 2018-05-13 09:49:41
@Mohamed Lee
**scrollViewWillBeginDragging()** and **scrollViewWillEndDragging()** func to show & hide tableView
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
{
if (isScrollingStart)
{
isScrollingStart=NO;
[self scrollingStopped];
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (isScrollingStart)
{
isScrollingStart=NO;
[self scrollingStopped];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
isScrollingStart=YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
isScrollingStart=YES;
}
-(void)scrollingStopped
{
NSLog(@"Scrolling stopped");
}
https://stackoverflow.com/questions/50232299
复制相似问题