Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

TextView multi-line Dynamic height not working #46

Open
li1 opened this issue Mar 8, 2015 · 13 comments
Open

TextView multi-line Dynamic height not working #46

li1 opened this issue Mar 8, 2015 · 13 comments
Labels

Comments

@li1
Copy link

li1 commented Mar 8, 2015

Hi, when I run Chats and type more than one line, the textview's height does not adjust accordingly. Instead of expanding the view, it stretches under the keyboard.
(iOS 8)

@vrwim
Copy link

vrwim commented Mar 17, 2015

Having the same problem, trying to figure out what is going on...

@Joropo
Copy link

Joropo commented Mar 20, 2015

i third this, i'm having the same dilemma

@vrwim
Copy link

vrwim commented Mar 20, 2015

A solution that I have found is creating an NSLayoutConstraint with a height that you calculate from the content of the textbox. (in the shouldChangeTextInRange method of UITextViewDelegate)
See this answer of mine on stackoverflow: http://stackoverflow.com/a/29104626/1009013

@Joropo
Copy link

Joropo commented Mar 20, 2015

did you make the constraint programmatically or via storyboard? do you know if it's possible to have autolayout on strictly one view?

@vrwim
Copy link

vrwim commented Mar 20, 2015

I made mine in code, as it's easier there, I don't really know how to disable a constraint in a storyboard (never tried).

I guess having autolayout on one view could be possible, I don't know, I use autolayout for everything...

@Joropo
Copy link

Joropo commented Mar 21, 2015

ok awesome, where did you put that function in the acani code? also would you mind putting your constraint code? i'm fairly new to constraints in general, and would like to see how you did it. Also, is it possible to only have a constraint on the text field and nothing else? thank you so, so much!

@Joropo
Copy link

Joropo commented Mar 21, 2015

i keep running into layout constraint problems :/

@vrwim
Copy link

vrwim commented Mar 21, 2015

I didn't fix it in this code, I did it in mine. If you need help with something, ask it on www.stackoverflow.com and not here.

@Joropo
Copy link

Joropo commented Mar 21, 2015

i did ..and haven't gotten help

@Xendrez
Copy link

Xendrez commented Mar 31, 2015

Here's what I did in addition to @vrwim 's solution:
First I added a height constraint:

    self.textFieldHeightLayoutConstraint = NSLayoutConstraint(item: textView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 28)
    self.toolBar.addConstraint(textFieldHeightLayoutConstraint)

I changed the updateTextViewHeight method to work with the constraint:

    func updateTextViewHeight() {
        let oldHeight = textView.frame.height
        let newText = textView.text
        let newSize = (newText as NSString).boundingRectWithSize(CGSize(width: textView.frame.width - textView.textContainerInset.right - textView.textContainerInset.left - 10, height: CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: textView.font], context: nil)
        let heightChange = newSize.height + textView.textContainerInset.top + textView.textContainerInset.bottom - oldHeight

            let maxHeight = self.view.frame.height
            - self.topLayoutGuide.length
            - currentKeyboardHeight
            + toolBar.frame.height
            - textView.textContainerInset.top
            - textView.textContainerInset.bottom
            - 20

            if !(textFieldHeightLayoutConstraint.constant + heightChange > maxHeight){
                //ceil because of small irregularities in heightChange
                self.textFieldHeightLayoutConstraint.constant = ceil(heightChange + oldHeight)

                //In order to ensure correct placement of text inside the textfield:
                self.textView.setContentOffset(CGPoint.zeroPoint, animated: false)
                //To ensure update of placement happens immediately
                self.textView.layoutIfNeeded()

            }
            else{
                self.textFieldHeightLayoutConstraint.constant = maxHeight
            }

    }

Finally, I added the following to KeyboardDidShow method in order to support both landscape and portrait:

    //the var recentlyTransitionedSize is set within the viewWillTransitionToSize method
    if self.recentlyTransitionedSize == true {

            //Because notifications happen on background threads, any UI-changes has to be done on the main thread.
            dispatch_async(dispatch_get_main_queue(), {
                self.updateTextViewHeight()
            })
            self.recentlyTransitionedSize = false
        }

I hope this helps someone!

@kos9kus
Copy link

kos9kus commented Apr 6, 2016

I've looked into that problem, it seems the UIToolbar has an intrinsic size that doesn't permit change its frame explicitly. Solving a problem through a constraint it's not so elegant in that case, I'd say.
Does anyone know how to change intrinsic size of UIToolBar explicitly??

@bryankattah
Copy link

hi there, I am new to swift and I was just taking a look at the Chat Xcode project but can't find the main.storyboard. can you show me how to navigate to it

@ma11hew28
Copy link
Member

@bryankattah see issue #60.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants