patrickjquinn
patrickjquinn

Reputation: 288

iOS - How to reduce alpha of UINavigationBar titleView whilst scrolling UITableView?

I am looking for a way to reduce the UINavigationBar titleView alpha channel to zero whilst scrolling down a UITableView and do the reverse whilst scrolling up.

Is there any clean way to do so using the scrollViewDidScroll delegate?

Upvotes: 1

Views: 1257

Answers (2)

Mike Sand
Mike Sand

Reputation: 2770

Super simple, just drop this into a table view controller in a navigation controller:

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

Subtracting self.tableView.frame.size.height is because the contentOffset is set from the top, so even at the lowest position will be separated by width of the frame; the 0-100 range is 0 to the (contentSize.height - frame.size.height).

Upvotes: 7

Bienemann
Bienemann

Reputation: 152

You could use the contentOffset value of the scrollView to determine its distance from the top and therefore set the alpha accordingly.

Upvotes: 1

Related Questions