以编程方式扩展ScrollView的高度

时间:2018-08-09 23:55:58

标签: ios swift xcode scrollview

我现在正努力解决这个问题,这并不意味着很难,因为我是一个初学者。

我正在使用Swift快速处理XCode 9。

我创建了一个ViewController,其中包含一个包含Scroll视图的View。 我的目标是能够以编程方式设置滚动视图(可以滚动浏览)的模拟高度,以便使ScrollView展开。

我尝试过:

~/applicationform$ gunicorn --bind 0.0.0.0:8000 newProject.wsgi:application

将该视图的高度设置为3000,但是它不起作用。如果我使用情节提要板设置视图的高度,则它可以正常工作...

1 个答案:

答案 0 :(得分:0)

Scrollview希望它的内容来自内部元素,反之亦然,或者您需要像这样设置它的contentSize

let scroll = UIScrollView(frame: self.view.frame)

view.addSubview(scroll)

let v1 = UIView(frame: self.view.frame)

v1.backgroundColor = .red

let v2 = UIView(frame: self.view.frame.offsetBy(dx: 0, dy: self.view.frame.height)) // set y of view 2 to be bottom of view 1

v2.backgroundColor = .blue

scroll.addSubview(v1)

scroll.addSubview(v2)

scroll.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height * 2 )
相关问题