This is something basic I realized isn't clear to me.
Using this code:
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
scrollView.backgroundColor = .green
let searchBar = UISearchBar()
searchBar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
searchBar.heightAnchor.constraint(equalToConstant: 44.0),
searchBar.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width)
])
let size = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height + 44.0)
scrollView.addSubview(searchBar)
scrollView.contentSize = size
It seems the scroll view starts at the top. But when I add searchbar, it doesn't start at the top. Why is that?
Thanks!
UIScrollView has something called contentInsetAdjustmentBehavior
and the default value is .automatic
which means it does some "magical" things for you when you constraint it to the root view of the root view controller of a UIWindow.
These magical things include making the scroll view background stretch past the safe areas. When the scroll view hasn't scrolled at all yet, like in your case, the content itself of the scroll view will follow the safe areas but once you start scrolling (assuming you had a larger content size) you would see your search bar go under the safe area and then out of the screen. You can see what insets are being applied to your content by accessing adjustedContentInsets
which is usually the result of your specified contentInset
(if any) and the root view's safeAreaInsets
.
If you set the contentInsetAdjustmentBehavior
to .never
you will get the behavior you would intuitively expect. Although it's not recommended to do that since it will mess up with a lot of things including the scroll indicator insets so only do so if you have a clear understanding of the consequences of such change.
Side note: Your search bar possibly has an ambiguous vertical position since there's no y-axis constraint outside of the height.