I've created a UIScrollView
to show it in another UIViewController
with a header. In my scrollViewDidScroll()
, I have some code that decreases header's height. But when it performs, all of the elements (Label) in header view changes. Is there any way to keep their dimensions fix?
my scrollViewDidScroll()
function is here:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
var labelAlpha = 4 * scrollView.contentOffset.y / scrollView.frame.height
labelAlpha = max(0, labelAlpha)
labelAlpha = min(1, labelAlpha)
let parent = self.parent as? ScrollParentViewController
var viewScale = 1.0 - 4 * scrollView.contentOffset.y / scrollView.frame.height
viewScale = max(0.5, viewScale)
parent?.redView.transform = CGAffineTransform(scaleX: 1, y: viewScale)
parent?.redView.frame.origin.y = 0
parent?.whiteLabel.alpha = labelAlpha
}
Finally found my answer. I'll post it for you to use if you need. For this case we shouldn't use transform. We can work with view.frame.size.height
. You should save view's height size in a variable at the first because its value will change.
so add it to your HeaderViewController
(In my case its name is ScrollParentViewController
):
var height: CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
height = redView.frame.height
}
then you can use this variable in your scrollViewDidScroll()
:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
var viewScale = 1.0 - 4 * scrollView.contentOffset.y / scrollView.frame.height
viewScale = max(0.5, viewScale)
let parent = self.parent as? ScrollParentViewController
parent?.redView.frame.size.height = (parent?.height)! * viewScale
}
thanks for helps.