textView高度等于内容大小而没有viewDidLayoutSubviews

时间:2015-05-02 22:01:31

标签: ios swift nslayoutconstraint

我正在创建一个viewController,它包含2个textViews标题和一个fullText。目前我已经在界面构建器中创建了2个textViews,它们放在彼此之下,然后在代码之后创建,以将高度更改为等于内容。然而问题是它似乎被延迟了,这给用户带来了糟糕的体验。延迟我的意思是它需要0.5或1秒才能调整大小?这是我的代码:

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()

    setHeightToContent(self.titleText!)
    setHeightToContent(self.fullText!)

    scrollView?.contentSize = CGSizeMake(self.view.frame.width, self.fullText!.frame.origin.y + self.fullText!.frame.height)
    println(self.fullText!.frame.origin.y + self.fullText!.frame.height)



}


func setHeightToContent(theTextView: UITextView) {

    let contentSize = theTextView.sizeThatFits(theTextView.bounds.size)
    var frame = theTextView.frame
    frame.size.height = contentSize.height
    theTextView.frame = frame

    var aspectRatioTextViewConstraint = NSLayoutConstraint(item: theTextView, attribute: .Height, relatedBy: .Equal, toItem: theTextView, attribute: .Width, multiplier: theTextView.bounds.height/theTextView.bounds.width, constant: 1)
    theTextView.addConstraint(aspectRatioTextViewConstraint)
}

1 个答案:

答案 0 :(得分:0)

根据自己的内容使每个文本视图自我调整大小,并使用约束,以便根据内容自动配置滚动视图contentSize