我正在使用Swift3
创建应用,并且我很难为UIScrollView
正确定义高度,我使用autolayout
并创建此结构:< / p>
UIScrollView
UIView
//容器view
UIImageView
// Constraint
热门边缘= 20与UIView
相关UITextView
// Constraint
热门边缘= 40与UIImageView
相关UITextView
// Constraint
热门边缘= 20与UITextView
相关UIButton
// Constraint
与UITextView
相关的热门边缘30 目前,我正在使用此逻辑来计算UIScrollView
身高
override func viewDidLayoutSubviews() {
var scrollHeight : CGFloat = 0.0
for view in self.containerView.subviews {
view.layoutIfNeeded()
scrollHeight += view.frame.size.height
}
// Adding height for scrollview, according to the sum of all child views
self.scrollView.contentSize.height = scrollHeight
}
但是,我只能得到views
高度,而他们不考虑Constraints
&#34;边距&#34;,我想知道任何方法来计算{{1}的正确高度根据其内容进行调整。
答案 0 :(得分:0)
关闭!让我们为每个视图添加上边距:
override func viewDidLayoutSubviews() {
var scrollHeight : CGFloat = 0.0
for view in self.containerView.subviews {
view.layoutIfNeeded()
scrollHeight += view.frame.size.height
//Add the margins as well to the scrollHeight
scrollHeight += view.layoutMargins.top
}
// Adding height for scrollview, according to the sum of all child views
self.scrollView.contentSize = CGRect(x: self.scrollView.contentSize.width, y: scrollHeight)
}
答案 1 :(得分:0)
self.scrollview.contentsize.height = containerview.height;