-2

How can I create a gradient just like shown in this pic which appears on top of tableview only after scrolling the tableview in Swift 4.2; and disappears at scrolled back to 0th cell index?

gradient on top of tableview https://drive.google.com/file/d/1eSzVYa_tI2_JEbP9MllAiXR7l1jN7bn-/view?usp=sharing

Mumtaz Hussain
  • 925
  • 9
  • 23

2 Answers2

0
  1. Add gradient shadow over table view
  2. Confirm to UITableViewDelegate protocol and tableView.delegate = self
  3. In scrollViewDidScroll(_:) hide / show gradient based on contentOffset aka:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    gradient.isHidden = scrollView.contentOffset > 20
}
ManWithBear
  • 2,787
  • 15
  • 27
  • hi @ManWithBear thanks man. Can you please tell me how I can add gradient shadow to top of tableview? I'd really appreciate the help. – Mumtaz Hussain Mar 24 '19 at 04:27
  • @MumtazHussain you [create gradient view](https://stackoverflow.com/questions/23074539/programmatically-create-a-uiview-with-color-gradient), then add it as subview and apply constraints to be on top of tableview – ManWithBear Mar 24 '19 at 11:08
0

I figured it out. I added a custom view with gradient effect (with changing alpha from top to bottom, 1.0 to 0.3 respectively) on 20pt below the top of tableview (so this view should overlap the tableview by 20pt), and gave tableview a top of UIEdgeInsets of 20 pt so at start tableview cells should stay below the gradient view. Moreover I moved the tableview (which is a subview) to back.

Mumtaz Hussain
  • 925
  • 9
  • 23