Ios/Swift - Hide/Show Uitabbarcontroller When Scrolling Down/Up

iOS/Swift - Hide/Show UITabBarController when scrolling down/up

This is code that i'm actually using in a production app.

It's in Swift and it also updates UITabBar.hidden var.

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
changeTabBar(hidden: true, animated: true)
}
else{
changeTabBar(hidden: false, animated: true)
}
}

You can also use the other callback method:

func scrollViewDidScroll(scrollView: UIScrollView) {
...
}

but if you choose so, then you must handle multiple calls to the helper method that actually hides the tabBar.

And then you need to add this method that animates the hide/show of the tabBar.

func changeTabBar(hidden:Bool, animated: Bool){
var tabBar = self.tabBarController?.tabBar
if tabBar!.hidden == hidden{ return }
let frame = tabBar?.frame
let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!)
let duration:NSTimeInterval = (animated ? 0.5 : 0.0)
tabBar?.hidden = false
if frame != nil
{
UIView.animateWithDuration(duration,
animations: {tabBar!.frame = CGRectOffset(frame!, 0, offset)},
completion: {
println($0)
if $0 {tabBar?.hidden = hidden}
})
}
}

Update Swift 4

func changeTabBar(hidden:Bool, animated: Bool){
guard let tabBar = self.tabBarController?.tabBar else { return; }
if tabBar.isHidden == hidden{ return }
let frame = tabBar.frame
let offset = hidden ? frame.size.height : -frame.size.height
let duration:TimeInterval = (animated ? 0.5 : 0.0)
tabBar.isHidden = false

UIView.animate(withDuration: duration, animations: {
tabBar.frame = frame.offsetBy(dx: 0, dy: offset)
}, completion: { (true) in
tabBar.isHidden = hidden
})
}

Hide tab bar when tableview scroll in swift 4

Your Tabbar frame is not setting correctly. Modify your code as following. It works

 //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
}
}

Hiding the tabbar and removing the space

If you're still seeing a black stripe under your hidden tab bar, have you tried to select Extend Edges Under Opaque Bars here?

Sample Image

Make also sure that Under Bottom Bars is still selected. Hope it helps!

(Xcode / Swift) How to hide toolbar when scrolling down UIWebView?

You can use UIScrollViewDelegate for that.

Here is example code for hide navigation bar and tool bar with scroll:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var toolBar: UIToolbar!
@IBOutlet weak var webV: UIWebView!
var lastOffsetY :CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()

webV.scrollView.delegate = self
let url = "http://apple.com"
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webV.loadRequest(request)
}

//Delegate Methods
func scrollViewWillBeginDragging(scrollView: UIScrollView){
lastOffsetY = scrollView.contentOffset.y
}

func scrollViewWillBeginDecelerating(scrollView: UIScrollView){

let hide = scrollView.contentOffset.y > self.lastOffsetY
self.navigationController?.setNavigationBarHidden(hide, animated: true)
toolBar.hidden = hide
}
}

Swift : show searchbar when scroll up and hide it when scroll down

All you need to do is to hide and display your search bar when you start scrolling so for that you need to override the didBeginScrolling (check the exact name ) and add this code accordingly

navigationItem.hidesSearchBarWhenScrolling = false

more info is here https://stackoverflow.com/a/46352230/5123516

Swift UITabBarController hide with animation

You could change the tab bar's frame inside an animation, so something like:

func hideTabBar() {
var frame = self.tabBarController?.tabBar.frame
frame?.origin.y = self.view.frame.size.height + (frame?.size.height)!
UIView.animate(withDuration: 0.5, animations: {
self.tabBarController?.tabBar.frame = frame!
})
}

func showTabBar() {
var frame = self.tabBarController?.tabBar.frame
frame?.origin.y = self.view.frame.size.height - (frame?.size.height)!
UIView.animate(withDuration: 0.5, animations: {
self.tabBarController?.tabBar.frame = frame!
})
}

Which sets the tab bar just below the visible screen, so that it slides up/down from the bottom.

How to hide tab bar when dragging like Safari app?

Something like this should work. I don't know if this gives exactly the same look as the Safari app, but it's close:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic) CGRect originalFrame;
@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 1000);
self.originalFrame = self.tabBarController.tabBar.frame;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
UITabBar *tb = self.tabBarController.tabBar;
NSInteger yOffset = scrollView.contentOffset.y;
if (yOffset > 0) {
tb.frame = CGRectMake(tb.frame.origin.x, self.originalFrame.origin.y + yOffset, tb.frame.size.width, tb.frame.size.height);
}
if (yOffset < 1) tb.frame = self.originalFrame;
}

hidesBarsOnSwipe with UITabBar

I solved the issue. I had embedded the UITabBarController inside a UINavigationController, which I had put as the root view controller for the window. After I made the root just the tab bar controller, it worked like a charm.

Thanks!



Related Topics



Leave a reply



Submit