gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:08:06 25 4
gpt4 key购买 nike

我正在尝试实现一个粘性标题功能,例如在 Twitter 个人资料中看到的功能。我已经根据设置了我的 ScrollView ,并且我一直在尝试研究如何这样做,但是,我只能找到使用 Storyboard的方法。我的代码将在下面。

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

最佳答案

像这样的东西应该有用(无论如何它对我有用):

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
}
}

关于swift - 如何以编程方式在 UIScrollView 中设置 StickyHeader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51229354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com