15.2 C
London
Wednesday, September 11, 2024

ios – My tableView Header isn’t getting stick on prime whereas scrolling, I am including a scrollView too at prime of tableView


That is my code

non-public let parentScrollView = UIScrollView()
non-public let scrollViewContentView = UIView()
non-public let tableView = UITableView(body: CGRect.zero, type: .plain)

// MARK: Setting Scroll View

extension ViewController{
non-public func setupParentScollView() {
    self.view.addSubview(parentScrollView)
    parentScrollView.delegate = self
    parentScrollView.isScrollEnabled = false
}

non-public func addParentScollViewConstraints() {
    parentScrollView.snp.makeConstraints { make in
        make.left.prime.proper.backside.equalTo(self.view.safeAreaLayoutGuide)
    }
}
}

// MARK: Setting Scroll View’s Content material View

extension ViewController{    
non-public func setupScollViewContentView() {
    self.parentScrollView.addSubview(scrollViewContentView)
}
non-public func addScollViewContentViewConstraints() {
    scrollViewContentView.snp.makeConstraints { make in
        make.edges.equalTo(parentScrollView) // Set constraints to cowl the complete scrollView.
        make.width.equalTo(parentScrollView)
    }
}
}

// MARK: – Setting Up TabelView

extension ViewController{     
non-public func setupTableView() {
    scrollViewContentView.addSubview(tableView)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
    tableView.showsVerticalScrollIndicator = false
    tableView.bounces = false
    tableView.contentInset = UIEdgeInsets(prime: 0, left: 0, backside: 100, proper: 0)
    print("that is from tableView",tableView.type)
    
    if #obtainable(iOS 15.0, *) {
        tableView.sectionHeaderTopPadding = 0
    }
}

func addTableViewConstraints() {
    tableView.snp.makeConstraints { (make) in
        make.prime.equalTo(searchBottomPaddingView.snp.backside).offset(100)
        make.left.proper.backside.equalTo(scrollViewContentView)
        make.top.equalTo(parentScrollView.snp.top)
    }
}
}




extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == tableView {
        let offsetY = tableView.contentOffset.y
        print("that is from scrollView", offsetY, parentScrollView.contentOffset.x)
        if offsetY > 0 && offsetY <= 120 {
            // Scrolling down, transfer the parentScrollView up by 120 factors
            parentScrollView.setContentOffset(CGPoint(x: parentScrollView.contentOffset.x, y: 120), animated: true)
        }
        
        if offsetY == 0{
            // Scrolling down, transfer the parentScrollView up by 120 factors
            parentScrollView.setContentOffset(CGPoint(x: parentScrollView.contentOffset.x, y: 0), animated: true)
        }
}

**right here, I do not wish to scroll the header of the tableView however since it’s contained in the contentView of scrollView it’s scrolling with it
I typically wish to get hold of parallax impact whereas scrolling, please assist me.
**

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here