滚动tableview时编辑导航栏的alpha

时间:2015-02-02 09:34:36

标签: ios objective-c xcode scroll uinavigationbar

我想询问当用户滚动tableview时是否可以更改导航栏的alpha值。

我有算法,但我需要一些帮助来实时更改。

  /* 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);

1 个答案:

答案 0 :(得分:7)

这可能为时已晚,但为了将来参考,你可以这样做:

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

在此委托中,您具有scrollview或tableView的contentOffset的值,您可以观察此属性的值以实现所需的行为。

希望它有所帮助!