gpt4 book ai didi

ios - 如何在 UIScrollView 中获取内容 View 以使用自动布局填充父级

转载 作者:行者123 更新时间:2023-11-29 00:57:30 25 4
gpt4 key购买 nike

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

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

Example of issue

一切都在代码中(除了最外面的父级,它具有固定的高度约束 50 和左右边距 0)

ScrollView 和内容 View 的约束:

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

最佳答案

尝试向 barScrollContentView 添加两个约束:居中 Y(在容器中垂直)和固定宽度。我认为这可能会解决您的问题。

关于ios - 如何在 UIScrollView 中获取内容 View 以使用自动布局填充父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com