UIView ignoring my constraints when setting width equal to scrollview?

喜你入骨 提交于 2021-01-29 07:54:32

问题


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.

  1. First, drag a scroll view to your storyboard and pin in to four sides.

  1. Then, add a view inside the scroll view and pin it to the four sides of its superview (i.e. the scroll view)

  1. The result would look like this

(Don't worry about the red markers, those will be fixes in the next steps)

  1. Add a constraint to make the view's width equal to its superview's (scrollview) width

The result would look like:

  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!