gpt4 book ai didi

ios - 将 View 放在 UIScrollview 的末尾 - Swift

转载 作者:行者123 更新时间:2023-11-30 13:52:07 25 4
gpt4 key购买 nike

我的目标是在 UIScrollView 的末尾(右下)设置一个 UIView。我看到使用 UIContentSize 的解决方案,但我不想使用此属性,因为它不适合: UIView 宽度不等于 UIScrollview contentSize。

UIView 是 UIScrollView 的 subview ,我只需要水平滚动。我认为我必须使用约束才能将 UIView 粘在 UIscrollview 的末尾(这意味着 UIView 右侧与 UIScrollView 右侧相同,两者具有相同的高度)

我所做的是:

let horizontalConstraint = NSLayoutConstraint(item: self.myView , attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.myScrollView , attribute:  NSLayoutAttribute.Right, multiplier: 1, constant: 0)

self.myScrollView.addConstraint(horizontalConstraint)

let verticalConstraint = NSLayoutConstraint(item: self.myView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.myScrollView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)

self.myScrollView.addConstraint(verticalConstraint)

我对如何设置这个约束有点困惑和困惑,我可能是错的,因为我的 View 不再显示。我需要定义所有约束还是仅定义右侧约束和顶部约束?有什么建议吗?谢谢

编辑:我想要的解决方案可以这样表示:enter image description here

最佳答案

UIScrollView 从它的 subview 中获取内容大小,因此它只会与它包含的内容一样大。我们无法独立设置它的大小。一切都应该在 subview 中。

决定您想要的 myScrollView 的大小:

let myViewWidth = myView.frame.width
let myViewHeight = myView.frame.height
let screenWidth = myScrollView.frame.width
let scrollContentWidth = myViewWidth + screenWidth

创建一个 subview ,我们称之为widthView

let widthView = UIView(frame: CGFloat(x: 0, y: 0, width: scrollContentWidth, height: myViewHeight))
myScrollView.addSubview(widthView)

let leadingEdgeConstraint = NSLayoutConstraint(item: widthView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: myScrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
myScrollView.addConstraint(leadingEdgeConstraint)

let trailingEdgeConstraint = NSLayoutConstraint(item: widthView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myScrollView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
myScrollView.addConstraint(trailingEdgeConstraint)

let widthContainerConstraint = NSLayoutConstraint(item: widthView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: scrollContentWidth)
myScrollView.addConstraint(widthContainerConstraint)

现在将您的 myView 添加到该 View :

widthView.addSubview(myView)
myView.translatesAutoresizingMaskIntoConstraints = false

let horizontalConstraint = NSLayoutConstraint(item: myView , attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: widthView , attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
widthView.addConstraint(horizontalConstraint)

let heightConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: myViewHeight)
widthView.addConstraint(heightConstraint)

let widthConstraint = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: myViewWidth)
widthView.addConstraint(widthConstraint)

关于ios - 将 View 放在 UIScrollview 的末尾 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34136611/

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