I am working with UIKit.
I have a main view that contains a few stack views + few label views + a horizontally scrollable collection view.
I am then adding this main view to a superview.
I would like to offset this main view's leading edge by x
amount, in such a way that all of its subviews are offset by this same amount, but when the horizontally scrollable collection view is scrolled, it can scroll all the way to the edges of the superview and not clip at the offset.
I would like to do this in a way that does not involve changing each of the main view's subviews offsets individually.
I think this can be done using SwiftUI safeAreaPadding, example: here. But as mentioned, I am using UIKit. Also, additionalSafeAreaInsets does not seem to do the trick.
How can I achieve this? Help is appreciated.
I am working with UIKit.
I have a main view that contains a few stack views + few label views + a horizontally scrollable collection view.
I am then adding this main view to a superview.
I would like to offset this main view's leading edge by x
amount, in such a way that all of its subviews are offset by this same amount, but when the horizontally scrollable collection view is scrolled, it can scroll all the way to the edges of the superview and not clip at the offset.
I would like to do this in a way that does not involve changing each of the main view's subviews offsets individually.
I think this can be done using SwiftUI safeAreaPadding, example: here. But as mentioned, I am using UIKit. Also, additionalSafeAreaInsets does not seem to do the trick.
How can I achieve this? Help is appreciated.
Share Improve this question asked Feb 15 at 8:07 luismpsluismps 431 silver badge5 bronze badges 1 |1 Answer
Reset to default 0Instead of adding mainView directly to superview, wrap it in a containerView that spans the full width of superview. Then, shift mainView within this container using a leading constraint. Example using UIKit -
class ViewController: UIViewController {
let superview = UIView()
let mainView = UIView()
let containerView = UIView()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
override func viewDidLoad() {
super.viewDidLoad()
// Add containerView to superview
superview.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: superview.topAnchor),
containerView.bottomAnchor.constraint(equalTo: superview.bottomAnchor),
containerView.leadingAnchor.constraint(equalTo: superview.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: superview.trailingAnchor)
])
// Add mainView to containerView with an offset
containerView.addSubview(mainView)
mainView.translatesAutoresizingMaskIntoConstraints = false
let offset: CGFloat = x // Update with desired value
NSLayoutConstraint.activate([
mainView.topAnchor.constraint(equalTo: containerView.topAnchor),
mainView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
mainView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: offset),
mainView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor)
])
// Add collectionView inside mainView (Assuming it takes full width)
mainView.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: mainView.topAnchor),
collectionView.bottomAnchor.constraint(equalTo: mainView.bottomAnchor),
collectionView.leadingAnchor.constraint(equalTo: mainView.leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: mainView.trailingAnchor)
])
// Adjust collectionView contentInset to allow full scrolling
collectionView.contentInset = UIEdgeInsets(top: 0, left: -offset, bottom: 0, right: 0)
}
}
.additionalSafeAreaInsets
to mimic SwiftUI's. safeAreaPadding
. Can you show us a minimal example of how you are setting up the constraints on your "few stack views + few label views + a horizontally scrollable collection view"? Depending on how you're going about that may affect the "best approach" to accomplish your goal. – DonMag Commented Feb 17 at 12:19