首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个SubViews调整ScrollView

使用多个SubViews调整ScrollView
EN

Stack Overflow用户
提问于 2015-10-13 13:43:14
回答 2查看 1.1K关注 0票数 0

我必须根据一些数据动态创建一些UILabel和UITextViews ~大约20个-它们都是动态高度/动态文本行的IOS应用程序在Swift。

由于屏幕不够大,我将视图添加到ScrollView,但不幸的是,我的ScrollView的contentsize属性似乎没有接收到正确的值。

我调试了几个小时,尝试了不同的设置,但到目前为止都没有成功。

调整大小是在一个自定义方法refreshUI()中完成的,该方法首先在viewDidLoad()中调用。ViewController只包含一个居中的标题标签和填充其余空间的ScrollView (固定在顶部、左侧、右侧、底部,并带有8.0)。然后,我尝试将数据填充到该scrollView中,如下所示:

代码语言:javascript
复制
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的大小一样大,因此不能垂直滚动。

EN

回答 2

Stack Overflow用户

发布于 2015-12-17 11:04:58

如果你使用的是AutoLayout,那么this教程对于实现scrollview是很有用的。

票数 0
EN

Stack Overflow用户

发布于 2015-12-19 02:45:13

当您在viewDidLoad()内部运行refreshUI()时,正在创建的子视图将使用父视图的接口生成器默认值,而不是设备上的实际大小。这可能就是为什么你的尺寸不是你所期望的。如果您在viewDidLayoutSubviews()内部运行refreshUI(),那么子视图将正确地读取父视图的宽度和高度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33094958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档