make UIView in UIScrollView stick to the top when scrolled up

前端 未结 4 1320
春和景丽
春和景丽 2020-12-08 11:49

So in a UITableView when you have sections the section view sticks to the top until the next section overlaps it and then it replaces it on top. I want to have a similar eff

4条回答
  •  忘掉有多难
    2020-12-08 12:07

    Swift Solution based on EVYA's response:

    var navigationBarOriginalOffset : CGFloat?
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationBarOriginalOffset = navigationBar.frame.origin.y
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        navigationBar.frame.origin.y = max(navigationBarOriginalOffset!, scrollView.contentOffset.y)
    }
    

提交回复
热议问题