如何在UIScrollView中获取contentview以使用autolayout填充父级

时间:2016-05-27 05:44:17

标签: ios swift uiscrollview

所以我有一个工作的UIScrollView,内容视图可以横向滚动没有问题。我已经跟踪了我可以找到的所有在线资源以及与滚动相关的所有内容......我的问题是内容视图中的内容不会填充到UIScrollViews底部。就像滚动条占据底部5-10像素一样,内容无法在那里延伸。

在附加的示例图像中可以看到文本没有沿整个条形对齐,所有项目都以相同的方式添加,唯一不同的是小滚动区域内的UIScrollView作为父项。令人沮丧的是,如果我设置框架和内容区域而不是使用自动布局约束,这不是问题。

Example of issue

一切都在代码中(除了大多数父级,其中固定高度约束为50,边距为0左右)

Scrollview和内容视图的约束:

    //
    // scroll constraint
    //
    barScrollView.translatesAutoresizingMaskIntoConstraints = false

    // left constraint
    let leftSideConstraint = NSLayoutConstraint(item: barScrollView, attribute: .Left, relatedBy: .Equal, toItem: menuBtn, attribute: .Right, multiplier: 1.0, constant: 0)
    self.addConstraint(leftSideConstraint)

    // top constraint
    let topSideConstraint = NSLayoutConstraint(item: barScrollView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0)
    self.addConstraint(topSideConstraint)

    // bottom constraint
    let botSideConstraint = NSLayoutConstraint(item: barScrollView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0)
    self.addConstraint(botSideConstraint)

    // right side constraint
    let rightSideConstraint = NSLayoutConstraint(item: barScrollView, attribute: .Right, relatedBy: .Equal, toItem: collapseBtn, attribute: .Left, multiplier: 1.0, constant: 0)
    self.addConstraint(rightSideConstraint)


    //
    // scroll content constraint
    //
    barScrollContentView.translatesAutoresizingMaskIntoConstraints = false

    // left constraint
    let contentLeftSideConstraint = NSLayoutConstraint(item: barScrollContentView, attribute: .Left, relatedBy: .Equal, toItem: barScrollView, attribute: .Left, multiplier: 1.0, constant: 0)
    barScrollView.addConstraint(contentLeftSideConstraint)

    // top constraint
    let contentTopSideConstraint = NSLayoutConstraint(item: barScrollContentView, attribute: .Top, relatedBy: .Equal, toItem: barScrollView, attribute: .Top, multiplier: 1.0, constant: 0)
    barScrollView.addConstraint(contentTopSideConstraint)

    // bottom constraint
    let contentBotSideConstraint = NSLayoutConstraint(item: barScrollContentView, attribute: .Bottom, relatedBy: .Equal, toItem: barScrollView, attribute: .Bottom, multiplier: 1.0, constant: 0)
    barScrollView.addConstraint(contentBotSideConstraint)

    // right side constraint
    let contentRightSideConstraint = NSLayoutConstraint(item: barScrollContentView, attribute: .Right, relatedBy: .Equal, toItem: barScrollView, attribute: .Right, multiplier: 1.0, constant: 0)
    barScrollView.addConstraint(contentRightSideConstraint)

并且内部的所有项目都添加如下:

/**
 Add constraints to an item used in the bar

 - parameters:
   - newElement: The new element that is added
   - leftElement: The element to the left of the new element
   - parent: The parent view the constraint is added to
   - width: The size of the new element
   - isFirstItem: Boolean indicating if this item is the furtest item to the left side
 in the current container
 */
func addConstraintToItem(newElement: UIView, leftElement: UIView, parent: UIView, width: CGFloat, isFirstItem: Bool) {
    newElement.translatesAutoresizingMaskIntoConstraints = false

    // Width constraint
    let widthConstraint = NSLayoutConstraint(item: newElement, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: width)
    parent.addConstraint(widthConstraint)

    // Height constraint
    //let heightConstraint = NSLayoutConstraint(item: newElement, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.frame.height)
    //parent.addConstraint(heightConstraint)

    // left constraint
    var leftSideConstraint: NSLayoutConstraint

    if isFirstItem {
        leftSideConstraint = NSLayoutConstraint(item: newElement, attribute: .Left, relatedBy: .Equal, toItem: leftElement, attribute: .Left, multiplier: 1.0, constant: 0)

    } else {
        leftSideConstraint = NSLayoutConstraint(item: newElement, attribute: .Left, relatedBy: .Equal, toItem: leftElement, attribute: .Right, multiplier: 1.0, constant: 0)
    }
    parent.addConstraint(leftSideConstraint)

    // top constraint
    let topSideConstraint = NSLayoutConstraint(item: newElement, attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1.0, constant: 0)
    parent.addConstraint(topSideConstraint)

    // bot constraint
    let botSideConstraint = NSLayoutConstraint(item: newElement, attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1.0, constant: 0)
    parent.addConstraint(botSideConstraint)
}

1 个答案:

答案 0 :(得分:1)

尝试向barScrollContentView添加另外两个约束:Center Y (Vertically in container) and fixed width。这可能会解决你的问题。