1

I created a stack view with 2 buttons called

Services and Customize

My goal is to make this stack view disappear when I scroll down and make it appear again when I scroll up. Everything works as it should be when I scroll down but when I scroll up, the stack view, I named it

menuStack

it overlaps with the tableview. The tableView is the one that contains images of a shoe as you can see in the screenshot.

I tried this delegate for scrollview but I think something is missing in my anchors. Here are 3 screenshots of each events.

This is the default view

When I scroll down

When I scroll up the menuStack does not reappear

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    self.menuStack.translatesAutoresizingMaskIntoConstraints = false

    if targetContentOffset.pointee.y < scrollView.contentOffset.y {
        //scroll up

        self.menuStack.isHidden = true
        if #available(iOS 11.0, *) {
            let safeGuide = self.view.safeAreaLayoutGuide
            self.menuStack.topAnchor.constraint(equalTo: safeGuide.topAnchor).isActive = true
            self.menuStack.bottomAnchor.constraint(equalTo: self.tableView.topAnchor, constant: 50).isActive = true
            self.tableView.topAnchor.constraint(equalTo: safeGuide.topAnchor, constant: 100).isActive = true
        } else {
            // Fallback on earlier versions
        }


        self.menuStack.isHidden = false

    } else {
        //scroll down
        self.menuStack.isHidden = true
        if #available(iOS 11.0, *) {
            let safeGuide = self.view.safeAreaLayoutGuide
            self.tableView.topAnchor.constraint(equalTo: safeGuide.topAnchor).isActive = true
        } else {
            // Fallback on earlier versions
        }
    }
}
Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
松田Mr.
  • 5
  • 5
  • When exactly do you want the stack view to appear again? Is it as someone scrolls up or is it when you reach the top of the scroll view again? – Alan S Oct 09 '19 at 08:17
  • @Alan S Hi Alan, I would like for the stack to disappear when I scroll down. It is a header. When I scroll back up, I want the header or stack to reappear. – 松田Mr. Oct 09 '19 at 08:24

1 Answers1

1

It seems like the scrollViewDidEndDragging function is called when the user lifts their finger off the tableView, this happens before the tableView starts it's deceleration animation. I think you should use scrollViewDidScroll instead. I tried this and it's working for me:

class ViewController: UIViewController {
    @IBOutlet weak var menuStackView: UIStackView!
    @IBOutlet weak var tableViewTopConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.dataSource = self
            tableView.delegate = self
        }
    }
}

extension ViewController: UITableViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        menuStackView.isHidden = scrollView.contentOffset.y > CGPoint.zero.y
        tableViewTopConstraint.constant = (scrollView.contentOffset.y > CGPoint.zero.y) ? 0 : 40

    //CGPoint.zero.y is when the contentOffset is at the top
    }
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
}

I'll post a screenshot of the UI as well so you can see how my design is set up.

Updated

Alan S
  • 594
  • 3
  • 13
  • Hi Alan, it is working but I wish for the whole stack to disappear. Although the buttons disappear, when I scroll down there is still a white space on top of the table view. I wish for the menuStack to disappear completely(without white container) just so you can only see the tableView. Hope my explanation makes sense. Let me know if i need to send a screenshot – 松田Mr. Oct 09 '19 at 08:52
  • @松田Mr. I see. I've added something small that does that. I didn't set the top of the tableView to the bottom of the stackview anymore but directly to the top of the safe area. I outlet this top constraint to the view controller and in scrollViewDidScroll I adjust the value of this constraint to achieve what you want. It looks jumpy now but you could animate this constraint change pretty easily. – Alan S Oct 09 '19 at 12:42
  • Also if this stuff helped you could you shoot me an upvote and if the solution works then possibly accept my answer – Alan S Oct 09 '19 at 12:43