At the top of scrollView, I have an imageView and i want it to be at the top of the screen but no matter what i tried i could not find a way to put it on the top of the screen. It starts under the statusBar. Here is how is it look:
and this is what i want:
Here are the constraints:
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.45), ]) }
I tried this but didn’t fix:
scrollView.contentInsetAdjustmentBehavior = .never
there is one solution that worked but it isnt the best approach i believe:
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: -130),
When I add a constant to the top anchor of the scrollView, it resolves the issue. However, on other screens such as iPads or iPhone X, I need to adjust this constant accordingly. I’m not sure if calculating the height of the status bar and then dynamically adjusting the constraints is the the best approach. I feel like there must be an easier way.
2
You are shooting yourself in the foot:
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
That means that the top of the scroll view is the bottom of the status bar. Thus the image, which is inside the scroll view, is also below the bottom of the status — and since the scroll view clips its content, scrolling the image can never make it appear behind the status. Well, if you don’t want that, don’t say that!
Instead, pin the top of the scroll view to the top of its superview, and also configure the automatic content inset adjustment so that the top of the image is not automatically placed initially at the bottom of the status bar:
/// ...
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
/// ...
scrollView.contentInsetAdjustmentBehavior = .never
3