疯狂地使用UIScrollView和情节提要约束

时间:2019-05-21 19:38:00

标签: swift uiscrollview storyboard

我真的很疯狂,试图以编程方式管理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

非常感谢!

2 个答案:

答案 0 :(得分:1)

如果要根据标签调整滚动视图的大小,则只需要设置正确的约束即可。我下载了您的代码,并且没有几个正确的约束,我就能使滚动视图滚动。

这是我在您的项目上做的图钉

1)滚动查看图钉:

enter image description here

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

enter image description here

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

enter image description here

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

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

 }

}

对我有用。

答案 1 :(得分:0)

嘿,我很确定您将无法以这种方式设置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)
    }
}

希望这会有所帮助。如果您要尝试做其他不同/更具体的事情,请告诉我。