问题
Every time I set the width of my view to my scroll view it pushes the green square off the screen.
回答1:
@belgrim I had had a rough time working with scroll views. I would like to share my work around.
- First, drag a scroll view to your storyboard and pin in to four sides.
- Then, add a view inside the scroll view and pin it to the four sides of its superview (i.e. the scroll view)
- The result would look like this
(Don't worry about the red markers, those will be fixes in the next steps)
- Add a constraint to make the view's width equal to its superview's (scrollview) width
The result would look like:
- Now, the most important part, we need to add our content inside the view and should properly define the y-constraints inside the view so that the scroll view scrolls based on the content. Here, I simply, add a label and define the constraints as:
(Also, I setup the number of lines as 0 and line break as wordwrap)
Finally, we get properly defined storyboard:
With long text defined in the label outlet, the final output we get:
I have shown a simple example adding a label, you can add other views as per your need, just make sure that you don't miss defining the y-constraints.
Hope this helps.
来源:https://stackoverflow.com/questions/54621361/uiview-ignoring-my-constraints-when-setting-width-equal-to-scrollview