Michel Marqués
Michel Marqués

Reputation: 161

Edit the alpha of navigation bar when scrolls tableview

I wanna ask if its possible to change the alpha value of a navigation bar when the user is scrolling a tableview.

I have the algorithm, but i need something help to get changes on real time.

  /* This is the offset at the bottom of the scroll view. */
CGFloat totalScroll = scrollView.contentSize.height - scrollView.bounds.size.height;

/* This is the current offset. */
CGFloat offset = - scrollView.contentOffset.y;

/* This is the percentage of the current offset / bottom offset. */
CGFloat percentage = offset / totalScroll;

/* When percentage = 0, the alpha should be 1 so we should flip the percentage. */
scrollView.alpha = (1.f - percentage);

Upvotes: 1

Views: 3178

Answers (1)

Renan Kosicki
Renan Kosicki

Reputation: 2900

It's probably too late, but for future reference, you could do something like this:

 func scrollViewDidScroll(scrollView: UIScrollView) {
     self.navigationController!.navigationBar.alpha = 1 - (self.tableView.contentOffset.y / (self.tableView.contentSize.height - self.tableView.frame.size.height));
 }

On this delegate you have the values of contentOffset of scrollview or tableView, and you can observe the value of this property to make the behavior you desire.

Hope it helps!

Upvotes: 12

Related Questions