0

I'm having a hard time with resizing a UITextView generated with code. I'm using swift, but examples in Obj-C are definitely welcomed. There seems to be two problems here, which are directly related to each-other:

  • A constraint is being ignored (Trailing)
  • The height calculation is off, probably because the constraint is being ignored.

Here's the code I'm using the generate the UITextView:

if !loaded.biography.isEmpty {
    biographyView = UITextView()
    biographyView?.translatesAutoresizingMaskIntoConstraints = false
    biographyView?.scrollEnabled = false
    biographyView?.text = loaded.biography
    biographyView?.textColor = UIColor.blackColor()
    print("Set text to: " + loaded.biography)

    biographyConstraints.append(
        NSLayoutConstraint(item: biographyView!, attribute: .LeadingMargin, relatedBy: .Equal, toItem: interactionView, attribute: .LeadingMargin, multiplier: 1, constant: 0)
    )

    biographyConstraints.append(
        NSLayoutConstraint(item: biographyView!, attribute: .TrailingMargin, relatedBy: .Equal, toItem: interactionView, attribute: .TrailingMargin, multiplier: 1, constant: 0)
    )

    biographyConstraints.append(
        NSLayoutConstraint(item: biographyView!, attribute: .Bottom, relatedBy: .Equal, toItem: interactionView, attribute: .Bottom, multiplier: 1, constant: 0)
    )

    interactionView.addSubview(biographyView!)
}

view.addSubview(interactionView)
NSLayoutConstraint.activateConstraints(interactionConstraints)
if !loaded.biography.isEmpty {
    NSLayoutConstraint.activateConstraints(biographyConstraints)

    let rect = biographyView?.sizeThatFits((biographyView?.bounds.size)!)

    print("Width of bio view is: " + String(biographyView?.bounds.width))
    NSLayoutConstraint.activateConstraints([
        NSLayoutConstraint(item: biographyView!, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: (rect?.height)!)
    ])

    print("Set height constraint to: " + String(rect?.height))

    print("Added interaction & bio & set constraints")

}

The output of this when ran is the following:

Set text to: TESTING TESTING 123 THIS SOME FAKE BIOGRAPHY, I KNOW IT'S NOT MUCH BUT IT'S PLACEHOLDER TEXT, THIS SHOULD BE THE END, OH WAIT, BUT HEY RANDOM EMOJI: 
Width of bio view is: Optional(0.0)
Set height constraint to: Optional(30.0)
Added interaction & bio & set constraints

The field is positioned correctly, however the height adjustment is incorrect and the text spans off the screen on the right side. Even though it seems to end at the Trailing margin, the text-field continues to only be a single line of text. I want the text field to adapt to the height of the text.

Can anyone help me figure this out?

Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • @matt I've done numerous searches, which is where my solution of using a CGRect and szeThatFits has come, from other SO answers. – Hobbyist Jan 25 '16 at 01:30
  • I apologize if I don't see what's new about your question. Self-sizing text views are well-covered ground. – matt Jan 25 '16 at 01:31
  • @matt - Because this "well marked ground" that you speak of, isn't fixing my issue. I've displayed that I'm using the generic fix that is provided in most of the answers, and have also tried "generic fix #2" ~ `let sizeThatFitsTextView = biographyView!.sizeThatFits(CGSizeMake(biographyView!.frame.size.width, CGFloat.max));` – Hobbyist Jan 25 '16 at 01:41
  • Also, the answer you linked to (`marked duplicated`) doesn't use auto-layout, granted getting the expected height should be the same, it apparently doesn't work identically. – Hobbyist Jan 25 '16 at 01:43
  • I really don't see what constraints have to do with it. I have an example in my book of a self-sizing text view, and it uses constraints and works fine: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p531selfSizingTextView/ch23p811selfSizingTextField/ViewController.swift – matt Jan 25 '16 at 02:01
  • have you tried calling layoutIfNeeded on interactionView? – null Jan 25 '16 at 02:13
  • I solved this issue by executing the height changing code using `dispatch_async(dispatch_get_main_queue()` which is strange, because I'm fairly sure `viewWillLoad` is executed on the main thread, and there's no logic that I see anywhere that would cause a detach, anyway. It works. – Hobbyist Jan 25 '16 at 13:19

0 Answers0