用 UIScrollView 和故事板约束发疯
Posted
技术标签:
【中文标题】用 UIScrollView 和故事板约束发疯【英文标题】:Going crazy with UIScrollView and storyboard constraints 【发布时间】:2019-10-08 06:18:50 【问题描述】:试图以编程方式管理 UIScrollView 内容大小,我简直快疯了。
我有一个滚动视图,里面有另一个视图,我需要放大最后一个,然后调整滚动视图的内容。
这是我用于调整滚动视图内容大小的扩展
extension UIScrollView
func updateContentView()
contentSize.height = subviews.sorted(by: $0.frame.maxY < $1.frame.maxY ).last?.frame.maxY ?? contentSize.height
我遵循了很多教程来尝试解决这个问题,但我认为我的故事板约束有问题......
有人可以帮我解决这个问题吗...
这里是完整的项目链接
https://github.com/BossOz/ScrollViewTest
非常感谢!
【问题讨论】:
到底是什么问题?是不是滚动视图内容大小设置不正确? 【参考方案1】:如果你想根据标签调整滚动视图的大小,你只需要设置正确的约束。我下载了您的代码,并且几乎没有正确的约束,我能够使滚动视图滚动。
这是我为你的项目制作的图钉
1) 滚动查看图钉:
2)内容视图(高度有250优先级值):
3) 标签(我还在情节提要中将 Lines 属性设置为 0):
这就是我修改你的代码的方式:
class ViewController: UIViewController
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
myLabel.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lobortis, nulla vitae lacinia tempus, nisl urna ultrices dui, eget sagittis velit massa id orci. Donec scelerisque tempus nibh nec aliquet. Duis nec aliquam ex, quis dictum elit. Proin blandit efficitur est imperdiet consectetur. Aenean sollicitudin sapien eget arcu consectetur, ut gravida turpis consequat. Mauris egestas odio pharetra diam gravida mattis. Donec scelerisque ut erat id ullamcorper. Nam at mollis turpis. Nullam sit amet fermentum sapien. Proin lacinia libero porta ligula dictum sagittis. Etiam ligula risus, mollis ut maximus vel, vulputate non dolor.In auctor laoreet laoreet. Interdum et malesuada fames ac ante ipsum primis in faucibus. In pulvinar tristique ipsum, vitae ultrices dui fringilla et. Duis id ex dapibus magna fringilla congue. Pellentesque condimentum, nisl at pellentesque tincidunt, orci eros laoreet nisl, tristique dictum elit dolor id magna. Pellentesque elementum fermentum ante, efficitur ullamcorper lectus accumsan id. Etiam et molestie felis. Duis pulvinar, augue eu tincidunt blandit, nisl risus congue purus, in tincidunt sem est ac enim. Maecenas posuere ultrices dolor eget volutpat. Curabitur cursus cursus fermentum. Phasellus ac arcu nunc. Etiam justo tortor, auctor id sagittis at, imperdiet in sapien. Integer tempus, lacus ac suscipit egestas, nibh ligula finibus urna, in facilisis risus est sit amet ante. Proin bibendum sodales magna eget rutrum.Donec tempor dolor sit amet dapibus euismod. Praesent id leo sit amet nisl consectetur finibus in et ex. Sed bibendum a magna quis ullamcorper. Cras in orci nisl. Phasellus finibus lectus risus, et eleifend lacus commodo vel. Phasellus tristique ante vitae malesuada malesuada. Ut eu ultrices sem. Suspendisse bibendum consequat sodales. Pellentesque nisl lectus, tincidunt vitae tortor vitae, vestibulum mollis ligula. Cras varius urna ac ultrices luctus. Sed dapibus, mauris vel efficitur maximus, velit ligula pharetra mauris, in ornare lectus turpis sit amet justo.Ut molestie a ex non auctor. Aliquam et est nisl. Curabitur tincidunt efficitur felis, ut eleifend arcu varius ac. Nullam ut mi id dolor viverra elementum sed mattis nibh. Phasellus facilisis ac arcu vitae efficitur. Vivamus ut massa est. Aliquam ac eros dictum, lobortis nisi nec, ullamcorper ligula. Suspendisse dignissim nisl in hendrerit consequat. Aliquam nec vehicula turpis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam vel nunc urna. Sed suscipit justo id facilisis posuere. Aenean rhoncus finibus leo nec elementum."
myLabel.sizeToFit()
这对我有用。
【讨论】:
是的!我错过了标签的底部空间限制!感恩!【参考方案2】:嘿,我很确定您无法以这种方式设置 UIScrollView
的大小,因为您无法更改单个高度/宽度。您可能必须将整个内容大小设置为新的CGSize
对象。我通常的做法如下。
// SETUP SCROLL VIEW
let scrollView = UIScrollView() // create a UIScrollView
scrollView.frame = CGRect(...) // set it's frame in x,y,width,height
self.view.addSubview(scrollView) // add it to the current view
// ADJUSTING IT'S CONTENT SIZE
scrollView.contentSize = CGSize(width: youWidthHere, height: yourHeightHere)
// ONLY CHANGING HEIGHT
scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: yourHeightHere)
来自您的代码
extension UIScrollView
func updateContentView()
contentSize = CGSize(width: contentSize.width, height: subviews.sorted(by: $0.frame.maxY < $1.frame.maxY ).last?.frame.maxY ?? contentSize.height)
希望这会有所帮助。如果您尝试做一些不同/更具体的事情,请告诉我。
【讨论】:
以上是关于用 UIScrollView 和故事板约束发疯的主要内容,如果未能解决你的问题,请参考以下文章
如何将UIButton粘贴到UIScrollView的底部? (Chatto框架)