Adding a view to a scroll view that will stretch to fill available width

非 Y 不嫁゛ 提交于 2019-12-31 02:59:06

问题


I am using Auto Layout in a storyboard, no code, and am having difficulties in getting the view inside the scroll view's content view to stretch to fill the device width. I understand the issue is an ambiguous scroll view width, but I'm not sure how to make it non-ambiguous when I want it to always stretch to fill available width (with some padding).

In a view controller, I added a scroll view with 4 constraints: top, bottom, leading, trailing to superview. I added a view to the scroll view that will act as the content view - all subviews will be added to the content view. It has 4 constraints: top, bottom, leading, trailing to scroll view. I then added the view I want to be visible (a simple red box that has a fixed height but stretches to fill the screen width) to the content view. Its constraints are: trailing to superview (15), leading to superview (15), top to superview (15), bottom to superview (15), and height equals 60.

This results in an ambiguous scroll view width, and the frames are misplaced - it wants to set the box view's width to 0.

How can I set this up so the box view stretches to fill the device screen, resolving the scroll view content size width ambiguity?


回答1:


The constraints you described in your question are equivalent to the following VFL:

  • The scroll view occupies the entire view

    H:|[scrollView]|
    V:|[scrollView]|
    
  • The red view is 60 pt tall and has a margin of 15 to the edges of the scroll view's contentSize:

    H:|-(15)-[redView]-(15)-|
    V:|-(15)-[redView(60)]-(15)-|
    

The red view is ambiguous because there is nothing that defines its width. (The horizontal constraints between the red view and the scroll view define the scroll view's contentSize, not the red view's width. See Apple Technical Note 2154.)

You resolve this ambiguity by adding a constraint that says that the red view is 30pt narrower than the main view. So add constraint between red view an the scroll view's superview by control-dragging from the red view to the scroll view (and this is probably easiest to do from the document outline):

Then choose "equal widths":

Having defined the red view to be the same width of the main view, you now have to alter that constraint to modify the constant to adjust for the margins you supplied between the red view and the scroll view's contentSize. Thus, select that constraint you just added and edit it, changing the constant to -30:

Frankly, that Interface Builder technique is a little cumbersome. It may be easier to illustrate how to do this programmatically:

view.addConstraint(NSLayoutConstraint(item: redView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1.0, constant: -30))


来源:https://stackoverflow.com/questions/28268761/adding-a-view-to-a-scroll-view-that-will-stretch-to-fill-available-width

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