I have a UIImageView and a bottom container view inside a UIScrollView.
I set the scrollView's contentInsetAdjustmentBehavior
to .never
because I want the scrollView's content to anchor to top of the view, such that the content of the scroll view, in this case the imageView, shows inside the top safe area.
The ImageView currently shows all the way to the top. However, the problem is that when the user scrolls down, and the bottomContainer
view meets the top Safe Area, the top of the bottomContainer
view stretches. The bottom container has all its constraint set for its subviews, from top to bottom. I don't understand why the bottomContainer
view would stretch vertically.
The constraint for these views are as follows:
// adds view as subview with `translatesAutoResizingMasksIntoConstraints = false
self.view.add(subviews: mainScrollView)
NSLayoutConstraint.activate([
self.mainScrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
self.mainScrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
self.mainScrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
self.mainScrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
self.mainScrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: self.view.widthAnchor)
])
// adds views as subviews with `translatesAutoResizingMasksIntoConstraints = false
self.mainScrollView.add(subviews: [self.profileImageView, self.bottomContainer])
// adds constraints to caller view's edges on axis
self.mainScrollView.make(views: [self.profileImageView, self.bottomContainer], flushAlongAxes: [.horizontal])
// Adds VLF constraints to views on the callers edges
self.mainScrollView.addConstraints(
withVisualFormat: "V:|[profileView(\(self.profileViewHeight))]-(-10)-[bottomContainer]-15-|",
views: ["profileView": self.profileImageView, "bottomContainer": self.bottomContainer])
I will attach a some screenshots to clarify what is happening:
Here is how it is supposed to look:
Here is how it stretches as the bottomContainer
scrolls under the SafeArea:
How do I prevent the bottomContainer
from stretching when it scrolls under the top safe area?