我必须根据一些数据〜20左右动态创建一些UILabel和UITextViews - 所有这些都具有动态高度/文本行,用于Swift中的IOS应用程序。
由于屏幕不够大,我将视图添加到ScrollView,但不幸的是我的ScrollView的contentsize属性似乎没有收到正确的值。
我调试了几个小时后尝试了不同的设置,但到目前为止,没有一个能够解决问题。
调整大小是在自定义方法refreshUI()中完成的,首先在viewDidLoad()中调用它。 ViewController只包含一个居中的标题标签和ScrollView,它填充剩余的空间(固定到Top,Left,Right,Bottom with 8.0)。 然后我尝试在滚动视图中填充我的数据,如下所示:
func refreshUI(){
println("refreshUI()")
questionnaireTitle.text = site.questionnaireName
let questionnaire = site.questionnaire
//removes all SubViews in ScrollView
clearView(scrollView)
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
for questionGroup in questionnaire{
//QUESTIONGROUP HEADING
let questionGroupHeading = UILabel()
questionGroupHeading.setTranslatesAutoresizingMaskIntoConstraints(false)
questionGroupHeading.text = questionGroup.questionsHeading
questionGroupHeading.sizeToFit()
scrollView.addSubview(questionGroupHeading)
viewStack.append(questionGroupHeading)
//QUESTION
for question in questionGroup.questions{
//QUESTION LABEL
let questionLabel = UILabel()
questionLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
questionLabel.text = question.text
questionLabel.numberOfLines = 0
questionLabel.lineBreakMode = .ByWordWrapping
questionLabel.sizeToFit()
scrollView.addSubview(questionLabel)
viewStack.append(questionLabel)
if question.type == "selector"{
//SELECTOR QUESTION
println("selector Question")
for statement in question.statements{
//TODO add Statement + Picker
}
}
else if question.type == "standard"{
//STANDARD QUESTION
println("standard question")
let answerLabel = UITextField()
answerLabel.placeholder = "here goes your answer"
answerLabel.sizeToFit()
answerLabel.backgroundColor = UIColor.whiteColor()
answerLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(answerLabel)
viewStack.append(answerLabel)
}
}
}
//setUpConstraints
var counter = 0
var height:CGFloat = 0.0
for view in viewStack{
let rightConst = NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .Equal, toItem: scrollView, attribute: .Right, multiplier: 1.0, constant: 0.0)
let leftConst = NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal, toItem: scrollView, attribute: .Left, multiplier: 1.0, constant: 0.0)
let widthConst = NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: scrollView.frame.width)
view.addConstraint(widthConst)
scrollView.addConstraint(leftConst)
scrollView.addConstraint(rightConst)
//pin first view to top of scrollView
if counter == 0{
let topConst = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: scrollView, attribute: .Top, multiplier: 1.0, constant: 0.0)
scrollView.addConstraint(topConst)
}
//pin all other views to the top of the previousView
else{
let topConst = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: viewStack[counter - 1], attribute: .Bottom, multiplier: 1.0, constant: 8.0)
scrollView.addConstraint(topConst)
}
counter++
height += view.bounds.height
}
let contentSize = CGSize(width: scrollView.frame.width, height: height)
scrollView.contentSize = contentSize
scrollView.contentOffset = CGPoint(x: 0, y: 0)
println("refreshUI() done")
}
ScrollView不能垂直滚动,但由于不在屏幕范围内,因此无法显示某些内容。 但它水平滚动,虽然我将每个视图的宽度设置为SCrollView的宽度,这应该意味着每个SubView都与ScrollView的大小一样大,因此不能垂直滚动。
答案 0 :(得分:0)
如果您使用的是AutoLayout,那么this教程将有助于实现scrollview。
答案 1 :(得分:0)
在refreshUI()
内运行viewDidLoad()
时,正在创建的子视图使用父视图的Interface Builder默认值而不是设备上的实际大小。这可能是您的尺寸不符合预期的原因。如果您在refreshUI()
内部运行viewDidLayoutSubviews()
,则子视图将正确读取父视图的宽度和高度。