Why does my text label relative vertical position move when user transitions from landscape to portrait orientation?

I have a detailed view controller. It is made up of dynamic, model based, text label subviews that are placed by code on the view within a scrollview with set spacing. I have anchored the last text label (lastLabel) to the scrollview bottom anchor with the following:

Code Block
scrollView.bottomAnchor.constraint(equalTo: lastLabel.bottomAnchor, constant: 0).isActive = true

The above line was added on the suggestion from a DTS ticket, and it solved some other label subview and scroll view issues.

However, the problem I'm having, on all device sizes by the way, is that if I start in portrait orientation, my lastLabel is displayed as expected with correct spacing to the previous (above) text label, but then when I rotate to landscape orientation, then rotate back to portrait orientation, the lastLabel is shifted down and spacing to the previous (above) label is increased.

If I navigate back to the parent view, then return again to the detail view in portrait orientation, the lastLabel is correctly positioned again relative to the previous (above) label.

I don't seem to be able to figure out how to fix this bug so that the lastLabel on the view always retains the proper spacing to the previous (above) label, even after transitioning from landscape to portrait orientation.

I do not see this problem when transitioning from portrait to landscape. The landscape orientation always displays the lastLabel in the correct relative position to the previous (above) label.
Answered by aandyzoom01 in 614015022
Had a good 1-on-1 lab session with DTS this morning. They gave me a couple of different options to fix this. It’s gonna take some work so I have to decide which approach to use. One of the approaches is to use some of the new Xcode 12 Beta APIs for iOS 14 to replace my current approach to my detail view controller and instead use an advanced collection view that might make things much simpler and let the collection view API handle all the spacing.
Hello Andy!

Scroll views are really hard to handle sometimes. Usually I try to set spaced elements inside a StackView and let it decide its size, then I place the it inside the ScrollView and constrain it to the frameLayoutGuide and contentLayoutGuide appropriately.

Do you have a project link you could post reproducing the issue? Would you be able to post it on GitHub?
You’re right! Definitely difficult to work with scrollviews and space elements and layout. I went through a lot of iterations of what I wanted to do for look and feel and decided I wanted to place my text label subviews using code rather than stacks and auto layout. I will try to give you a public link to my project on github as soon as I can.
My github repository is private. I’m not sure how to provide you access and still keep the repository private. I’m a novice and not very experienced working in github except through Xcode source control features.
Hmm, could it be the content-hugging priority on the second-to-last label? Maybe it’s low and that label is vertically stretching?

Either way, this is a great use case for the view hierarchy debugger:
https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/special_debugging_workflows.html

The view hierarchy debugger can help you visualize what’s going on with the view layouts, and hopefully illuminate where the problem may be.
Accepted Answer
Had a good 1-on-1 lab session with DTS this morning. They gave me a couple of different options to fix this. It’s gonna take some work so I have to decide which approach to use. One of the approaches is to use some of the new Xcode 12 Beta APIs for iOS 14 to replace my current approach to my detail view controller and instead use an advanced collection view that might make things much simpler and let the collection view API handle all the spacing.
Why does my text label relative vertical position move when user transitions from landscape to portrait orientation?
 
 
Q