1

Just by programmatically coding, I'm trying to achieve something like below image. TextView and ContainerView are embedded in scrollview although it is not shown in the image.

enter image description here

Here is my code for this.

override func viewDidLoad() {
    super.viewDidLoad()
    let margin = CGFloat(10)
    textView =  UITextView()
    textView.text = "asdadaasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadaadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadaddadd"
let contentSize = textView.sizeThatFits(textView.bounds.size)
textView.frame = CGRectMake(margin, 0, self.view.frame.width - 2 * margin, contentSize.height)


let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 50))
let containerView = UIView(frame: CGRectMake(margin, contentSize.height + margin, self.view.frame.width - 2 * margin, self.view.frame.height))

let button1 = UIButton(frame:  CGRectMake(margin, contentSize.height + containerView.frame.height + 2 * margin, (view.frame.width - 2 * margin)/2, 30))

scrollView.contentSize = CGSize(width: self.view.frame.width, height: button1.frame.origin.y + button1.frame.height + margin)

self.view.addSubview(scrollView)
scrollView.addSubview(textView)
scrollView.addSubview(containerView)

scrollView.backgroundColor = UIColor.blueColor()
containerView.backgroundColor = UIColor.yellowColor()
textView.backgroundColor = UIColor.greenColor()
}

The problem is that textview's height is not dynamic to show all content in textview.

I've confirmed that if I add below code in viewdidappear, it does make the height of textview dynamic but it is stacked under Container view because below code runs after ViewDidLoad configured everything.

let contentSize = textView.sizeThatFits(textView.bounds.size)
textView.frame = CGRectMake(margin, 0, self.view.frame.width - 2 * margin, contentSize.height)

What would be the best approach for this kind of problem? I think this is caused due to rendering order in viewdidload but I'm not 100% sure. It would be great if someone can talk about rendering order too.

2
  • Is there a reason you want/need to lay things out programmatically? That can make things very difficult. Why not just use constraints? Commented Feb 6, 2016 at 3:42
  • @Dan, Tried using constraints and it was more painful than writing in code. Commented Feb 6, 2016 at 5:53

1 Answer 1

1

To answer the original question, your call to sizeThatFits has no idea how tall the TextView should be because it has no idea how wide you want it to be. I think before the sizeThatFits line you want the following:

textView.frame.size.width = self.view.frame.width - 2 * margin
Sign up to request clarification or add additional context in comments.

2 Comments

That was it. But do I need to write this line after sizeThatFits again? textView.frame = CGRectMake(margin, 0, self.view.frame.width - 2 * margin, contentSize.height) I feel like I'm duplicating width part. Would there be a better way for refactoring this?
Well true the width part is duplicated there, but the other properties need to be set (x, y, height.) x & y are 0 by default though, so really you could replace that with textView.frame.size.height = contentSize.height

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.