根据滚动视图位置移动 UILabel
Move UILabel based on scrollview postion
我的 headerView(图像视图)内有一个 titleLabel,它会根据滚动视图的滚动改变其大小。我正在尝试根据滚动视图的位置移动 titlelabel,因为它调整到与导航栏大致相同的大小 我正在尝试将标签移动到反映导航标题的位置。这就是我目前正在尝试的方式。
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: 400)
self.imageHeightConstraint.isActive = true
self.scrollView.delegate = self
self.view.addSubview(self.scrollView)
scrollView.anchor(top: imageView.bottomAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: -10, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
self.scrollView.contentOffset = CGPoint(x: 0, y: -100)
ScrollViewDidScroll
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = 100 - (scrollView.contentOffset.y - 100)
let height = max(y, 100)
self.imageHeightConstraint.constant = height
self.eventTitleLabel.center.y = scrollView.contentOffset.y
}
您不仅需要更改 Y 值,还需要像这样更改所有框架
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = 100 - (scrollView.contentOffset.y - 100)
let height = max(y, 100)
self.imageHeightConstraint.constant = height
//self.eventTitleLabel.center.y = scrollView.contentOffset.y
self.eventTitleLabel.frame = CGRect(x: 0, y: scrollView.contentOffset.y , width: label.frame.width, height: label.frame.height)
}
在这里,X 和 Y 管理你的 scrollview 作为你想要的位置。
我的 headerView(图像视图)内有一个 titleLabel,它会根据滚动视图的滚动改变其大小。我正在尝试根据滚动视图的位置移动 titlelabel,因为它调整到与导航栏大致相同的大小 我正在尝试将标签移动到反映导航标题的位置。这就是我目前正在尝试的方式。
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: 400)
self.imageHeightConstraint.isActive = true
self.scrollView.delegate = self
self.view.addSubview(self.scrollView)
scrollView.anchor(top: imageView.bottomAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: -10, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
self.scrollView.contentOffset = CGPoint(x: 0, y: -100)
ScrollViewDidScroll
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = 100 - (scrollView.contentOffset.y - 100)
let height = max(y, 100)
self.imageHeightConstraint.constant = height
self.eventTitleLabel.center.y = scrollView.contentOffset.y
}
您不仅需要更改 Y 值,还需要像这样更改所有框架
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = 100 - (scrollView.contentOffset.y - 100)
let height = max(y, 100)
self.imageHeightConstraint.constant = height
//self.eventTitleLabel.center.y = scrollView.contentOffset.y
self.eventTitleLabel.frame = CGRect(x: 0, y: scrollView.contentOffset.y , width: label.frame.width, height: label.frame.height)
}
在这里,X 和 Y 管理你的 scrollview 作为你想要的位置。