I’m having trouble getting my ContentView
to show up inside my ScrollView
and I don’t know what I’m doing wrong to make it not visible. In my screenshot down below, the purple view is my ScrollView
which is showing perfectly, and as you will see in my code, my ContentView
is red. I tried to change my properties to lazy var
to see if that would work, but I’m still having the same issue. Am I doing something wrong in my programmatic UI for my ContentView
not to show up, or am I missing something? Thank you!
Screenshot of Problem
FindEmployeeJobRequestController
// MARK: - Properties
lazy var jobInfoCardView: ShadowCardView = {
let view = ShadowCardView()
view.backgroundColor = .white
view.addShadow()
view.setHeight(height: 320)
return view
}()
let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.backgroundColor = .darkPurpleTint
sv.isScrollEnabled = true
return sv
}()
let contentView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
// MARK: - Helper Functions
fileprivate func configureUI() {
view.addSubview(jobInfoCardView)
jobInfoCardView.anchor(top: circularProgressView.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor,
paddingTop: 52, paddingLeft: 24, paddingRight: 24)
jobInfoCardView.addSubview(scrollView)
scrollView.anchor(top: jobInfoCardView.topAnchor, left: jobInfoCardView.leftAnchor,
bottom: jobInfoCardView.bottomAnchor, right: jobInfoCardView.rightAnchor,
paddingTop: 4, paddingLeft: 4, paddingBottom: 4, paddingRight: 4)
jobInfoCardView.addSubview(contentView)
contentView.anchor(top: scrollView.contentLayoutGuide.topAnchor, left: scrollView.contentLayoutGuide.leftAnchor,
bottom: scrollView.contentLayoutGuide.bottomAnchor, right: scrollView.contentLayoutGuide.rightAnchor)
contentView.anchor(left: scrollView.frameLayoutGuide.leftAnchor, right: scrollView.frameLayoutGuide.rightAnchor)
}
UPDATE
I tried to add my contentView
to my scrollView
‘s subview, but it’s still not showing. I even tried to add an UIStackView
to my contentView
to see if that could make it visible, but still not visible.
Updated Screenshot
Updated Code
fileprivate func configureUI() {
scrollView.addSubview(contentView)
contentView.anchor(top: scrollView.contentLayoutGuide.topAnchor, left: scrollView.contentLayoutGuide.leftAnchor,
bottom: scrollView.contentLayoutGuide.bottomAnchor, right: scrollView.contentLayoutGuide.rightAnchor)
contentView.anchor(left: scrollView.frameLayoutGuide.leftAnchor, right: scrollView.frameLayoutGuide.rightAnchor)
waitingOnEmployeeStack.axis = .vertical
waitingOnEmployeeStack.distribution = .fillEqually
waitingOnEmployeeStack.spacing = 4
contentView.addSubview(waitingOnEmployeeStack)
waitingOnEmployeeStack.centerX(inView: contentView)
waitingOnEmployeeStack.anchor(top: contentView.topAnchor, paddingTop: 5)
}
3
Answers
I think you wanted to add the contentView as a subView of the scrollView.
I think you might want to add
contentView
asscrollView
‘s subview and notjobInfoCardView
‘s subviewCouple notes…
I strongly recommend using standard constraint syntax – at least while you’re learning. We don’t know if your
.anchor(...)
funcs are doing the right thing, and when you review your code it’s not entirely clear what might be happening. Once you’ve really gotten the hang of auto-layout, you may find it easier to use "helper funcs" (personally, I don’t).Also – use comments so both you and we know what your intent is.
Take a look at this…
You haven’t shown what
ShadowCardView
might be, so we’ll start with just a plainUIView
subclass:And an example controller class:
When run, it will look like this (green view is your
circularProgressView
and purple view is the scroll view):That’s pretty much what you are already getting. That’s because – at the moment –
ShadowCardView
has no content to control its size. So, it’s not even visible.Let’s change
ShadowCardView
to this:We’ve added a vertical stack view with 30 labels (along with proper constraints), and the output is now:
and we can scroll: