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.

screenshot

+3


source to share


4 answers


You can use automatic layout to set the width of your subview to your main view by dragging a control from your subview to your main view, as shown below:

enter image description here



Then select "Equal Widths" from the popup

enter image description here

+9


source


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!

0


source


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.

0


source


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

0


source







All Articles