How to make ScrollView width = device screen width?
I am creating an application that has a (child) view inside a scroll. I understand that I have to explicitly set the width of the child view since the scroll view will not provide this information to the child, but how can I make this setting the width of this child based on the width of the device?
For example, below I set a width of 380, which centers the Quotes on the iPhone 6, but makes them slightly right-centered on the iPhone 5.
source to share
Select viewController
Stretch the view you want to fit the entire screen to fit the screen.
Click the center icon of the three options at the bottom of the window.
The icon appears.
The top 4 values ββwill have an extreme red line connecting them
click them all
at the bottom of the popup you will see "add 4 constraints"
click
Done!
source to share
You can remove the width limitation. The view will place it itself horizontally due to the leading and trailing viewing space. No specific width needed.
I think your scrollview will scroll vertically, so in this case, you will probably need to provide a constraint on the height or your view and update the content of the scroll to reflect that.
source to share
Since your child view is located in the view hierarchy of your view controller's view, you can assign constraints between your child view and the view controller's view.
Drag the image of your child to the controller view. You will see that they will be of equal width. Choose it. Then adjust the constant if you want your child's view width to be lower.
Then you can add base and end constraints between your child's view and the scrolling view.
Doing it this way will also ensure that you set the width of the scroll content to view dynamically and correctly.
Let me know if it works for you
source to share