【问题标题】:How to get contentview inside UIScrollView to fill parent with autolayout如何在 UIScrollView 中获取 contentview 以使用自动布局填充父级
【发布时间】:2016-05-27 05:44:17
【问题描述】:

所以我有一个工作的 UIScrollView,它的内容视图可以水平滚动,没有问题。我已经关注了我能找到的所有在线资源以及与滚动相关的所有内容......我的问题是放在 contentview 中的内容没有填充到 UIScrollViews 底部。就像滚动条占据了底部的 5-10 像素,使得内容无法在那里拉伸。

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

一切都在代码中(除了最外的父级,它的高度限制为 50,左右边距为 0)

滚动视图和内容视图的约束:

    //
    // 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)
}

【问题讨论】:

  • 您可以尝试隐藏滚动指示器以检查它是否导致标签压缩? self.tableView.showsHorizo​​ntalScrollIndicator = false。还有,可以打印barScrollContentView、elementView和底部标签的frame吗?检查其中哪些没有达到预期的高度。

标签: ios swift uiscrollview


【解决方案1】:

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

【讨论】:

  • 固定宽度是不可能的,因为我有一个水平滚动视图,固定高度使滚动冻结,我会尝试再次居中(之前尝试过,但当时有其他问题)跨度>
  • 你应该给定宽度。您可以在需要时通过增加该约束的常数来增加它。当您添加新视图时,增加宽度约束的常量作为添加视图的大小
  • 是的,但这并不是真正的动态方法……我想尝试使用自动布局来调整大小。无论如何,centerY做到了:D,不需要其他任何东西,这只是缺少的和平。非常感谢。
猜你喜欢
  • 2023-03-24
  • 2021-11-29
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多