本文介绍了疯狂使用 UIScrollView 和故事板约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图以编程方式管理 UIScrollView 内容大小,我真的快疯了.

我有一个滚动视图,里面有另一个视图,我需要放大最后一个视图,然后调整滚动视图内容.

这里是我用来调整滚动视图内容大小的扩展

扩展 UIScrollView {功能更新内容视图(){contentSize.height = subviews.sorted(by: { $0.frame.maxY < $1.frame.maxY }).last?.frame.maxY ??内容大小.高度}}

为了尝试解决这个问题,我学习了很多教程,但我认为我的故事板约束有问题...

有人可以帮助我吗...

这里是完整的项目链接

2) 内容视图(高度有 250 优先级值):

3) 标签(我还在故事板中将 0 设置为 Lines 属性):

这就是我修改你的代码的方式:

class ViewController: UIViewController {@IBOutlet 弱变量滚动视图:UIScrollView!@IBOutlet 弱变量 contentView:UIView!@IBOutlet 弱变量 myLabel:UILabel!覆盖 func viewDidLoad() {super.viewDidLoad()//加载视图后进行任何其他设置.myLabel.text = "Lorem ipsum dolor sat 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 exquit aliquet. Duis. 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 uterat id Namocorpiona libemocorp.ligula dictum sagittis. Etiam ligula risus, mollis ut maximus vel, vulputate non dolor.In auctor laoreet laoreet. Interdum etmalesuada 在 faucibus 中享有 ac ante ipsum primis.congue. Pellentesque 调味品, nisl at pellentesque tincidunt, orci eros laoreet nisl, tristique dictum elit dolor id magna. Pellentesque elementumfermentum 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 发酵.Phasellus ac arcu nunc.Etiam justo Tortor,auctor id sagittis at,imperdiet in sapien.整数 tempus,lacus ac suscipit egestas,nibh ligula finibus urna,在 facilisis risus est 坐 amet ante.Proin bibendum sodales magna eget rutrum.Donec tempor dolor 坐 amet dapibus euismod.Praesent id leo 坐在 amet nisl consectetur finibus in et ex.Sed bibendum a magna quis ullamcorper.在 orci nisl 中的 Cras.Phasellus finibus lectus risus, et eleifend lacus commodo vel.Phasellus tristique ante vitaemalesuadamalesuada.Ut eu ultrices sem.Suspendisse bibendum consequat sodales.Pellentesque nisl lectus,tincidunt vitae totortor vitae,前庭 mollis ligula.Cras varius urna ac ultrices luctus.Sed dapibus, mauris vel efficitur maximus, velit ligula pharetra mauris, in ornare lectus turpis 坐在 amet justo.Ut moestie a ex non aucor.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.类 aptent taciti socialsqu ad litora Tornt per conubia nostra, per inceptos himenaeos.Etiam vel nunc urna.sed suscipit justo id facilisis posuere.Aenean rhoncus finibus leo nec elementum."myLabel.sizeToFit()}}

这对我有用.

I'm literally going crazy trying to manage a UIScrollView content size programatically.

I have a scroll view with another view inside and I need to enlarge this last one and then adapt the scroll view content.

Here the extension I'm using for adapt the scroll view content size

extension UIScrollView {
    func updateContentView() {
        contentSize.height = subviews.sorted(by: { $0.frame.maxY < $1.frame.maxY }).last?.frame.maxY ?? contentSize.height
    }
}

I followed a lot of tutorials in order to try to fix this but I think a have a problem with my storyboard constraints...

Could some one help me on this...

Here the complete project link

https://github.com/BossOz/ScrollViewTest

Thanks so much!

解决方案

If you want to resize the scroll view depending on the label you only need to set the correct constraint. I downloaded your code and with few correct constraint i was able to make the scroll view scroll.

Here the pin i made on your project

1) Scroll view pin:

2) Content view (the height has 250 Priority value):

3) Label (i also set 0 as Lines property in storyboard):

And this is how i modified your code:

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()

 }

}

That's worked for me.

这篇关于疯狂使用 UIScrollView 和故事板约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:17