1

I am trying to custom a extension for Parallax Header. However, it's not working perfectly. The table header view always floats and overlaps cells.

enter image description here

Extension code:

extension UITableView {

  func addImageHeaderView(headerView headerView: UIView, height: CGFloat) {
    self.contentInset = UIEdgeInsetsMake(height, 0, 0, 0)
    self.contentOffset = CGPoint(x: 0, y: -height)
    self.tableHeaderView = headerView
    self.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: height)
  }

  func updateHeaderView(height kTableHeaderHeight: CGFloat) {

    var headerRect = CGRect(x: 0, y: -kTableHeaderHeight , width: self.bounds.width, height: kTableHeaderHeight)
    if self.contentOffset.y < -kTableHeaderHeight {
      headerRect.origin.y = self.contentOffset.y
      headerRect.size.height = -self.contentOffset.y
    }
    self.tableHeaderView?.frame = headerRect
  }

}

Implementing Code :

tableView.addImageHeaderView(headerView: viewHeader, height: 100)

func scrollViewDidScroll(scrollView: UIScrollView) {
      tableView.updateHeaderView(height: 200)
    }

Am I wrong at something? Please show me if you know.

AnLT
  • 569
  • 8
  • 22

1 Answers1

0

Can you please try to set the following in viewDidLoad

self.edgesForExtendedLayout = UIEdgeInsetsZero;

in swift

self.edgesForExtendedLayout = .None

Also what you can try to do is to check the result of the following

tableView.addImageHeaderView(headerView: viewHeader, height: 0)

func scrollViewDidScroll(scrollView: UIScrollView) {
      tableView.updateHeaderView(height: 200)
    }

please notice that i changed the height from 100 to 0 i did it because the height value will change the contentInset of your table view and this is exactly the distance from the top corner

Ran Hassid
  • 2,788
  • 1
  • 12
  • 20
  • Cannot assign value of type 'UIEdgeInsets' to type 'UIRectEdge'. Got this error. – AnLT Jul 26 '16 at 18:19
  • Thanks! I found it but no effect. :| – AnLT Jul 26 '16 at 18:25
  • can you please try the following: self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) self is your tableView and when you change its contentSet it will be the distance from top – Ran Hassid Jul 26 '16 at 18:26
  • func addImageHeaderView(headerView headerView: UIView, height: CGFloat) { self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) ... } Is that right? – AnLT Jul 26 '16 at 18:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118347/discussion-between-an-le-and-ran-hassid). – AnLT Jul 26 '16 at 18:33
  • Now, I have new task to custom a lib (http://stackoverflow.com/questions/38607639/discussion-swift-popup-tooltip) look like this https://github.com/ruipfcosta/SwiftyWalkthrough . However, they want me code a lib which they can pass the views and my lib will do everything, step by step. (cwl). I think I should write a letter of resignation. – AnLT Jul 28 '16 at 17:58