如何在UIScrollView中以编程方式设置StickyHeader

时间:2018-07-08 06:13:21

标签: swift xcode uiscrollview header sticky

我正在尝试实现粘性标头功能,例如在twitters个人资料上看到的功能。我已经根据我的需要设置了滚动视图,并且一直在尝试研究如何做到这一点,但是,我只能找到使用情节提要的方法,下面是我的代码。

class EditProfileVC: UIViewController {
var imageView: UIImageView!
var image = UIImage(named: "work")


lazy var scrollView: UIScrollView = {
    let view = UIScrollView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.contentSize.height = 800
    view.backgroundColor = UIColor.brown

    return view
}()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(scrollView)
    setupScrollView()
    view.addSubview(profileImageView)
        setupProfileImageView()

}

func setupScrollView(){

    imageView = UIImageView(image: image)
    scrollView.addSubview(imageView)
    scrollView.contentSize = imageView.bounds.size
    view.addSubview(scrollView)

    imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    imageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 200).isActive=true
    imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true

    imageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    let firstLabel = UILabel()
    firstLabel.translatesAutoresizingMaskIntoConstraints = false
    firstLabel.textColor = .white
    firstLabel.text = "Top of our ScrollView"

    scrollView.addSubview(firstLabel)

    firstLabel.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
    firstLabel.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 20).isActive = true
    firstLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
    firstLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true

有人可以告诉我我遗漏的内容并可能帮助我构造标题吗?如果Sticky Header非常复杂,则UIScrollView中的简单Header会有所帮助。先感谢您, 约翰         Twitter profile sticky header twitter profile sticky header

1 个答案:

答案 0 :(得分:3)

类似的事情应该起作用(无论如何对我来说是这样):

class ViewController: UIViewController, UIScrollViewDelegate {
    lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: #imageLiteral(resourceName: "Image"))
        imageView.clipsToBounds = true

        return imageView
    }()

    var imageHeightConstraint: NSLayoutConstraint!

    lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.contentSize = CGSize(width: 1200, height: 1200)
        view.backgroundColor = UIColor.brown
        view.contentInsetAdjustmentBehavior = .never
        view.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)

        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.scrollView.delegate = self
        self.view.addSubview(self.scrollView)
        self.scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

        let testView = UIView()
        testView.backgroundColor = .green
        self.scrollView.addSubview(testView)
        testView.translatesAutoresizingMaskIntoConstraints = false
        testView.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor).isActive = true
        testView.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor, constant: 150).isActive = true
        testView.topAnchor.constraint(equalTo: self.scrollView.topAnchor).isActive = true
        testView.heightAnchor.constraint(equalToConstant: 100).isActive = true

        self.view.addSubview(self.imageView)
        self.imageView.translatesAutoresizingMaskIntoConstraints = false
        self.imageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.imageView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.imageView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

        self.imageHeightConstraint = self.imageView.heightAnchor.constraint(equalToConstant: 100)
        self.imageHeightConstraint.isActive = true

        self.scrollView.contentOffset = CGPoint(x: 0, y: -100)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let y = 100 - (scrollView.contentOffset.y + 100)
        let height = max(y, 100)
        self.imageHeightConstraint.constant = height
    }
}