-
Notifications
You must be signed in to change notification settings - Fork 452
TextView multi-line Dynamic height not working #46
Comments
Having the same problem, trying to figure out what is going on... |
i third this, i'm having the same dilemma |
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) |
did you make the constraint programmatically or via storyboard? do you know if it's possible to have autolayout on strictly one view? |
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... |
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! |
i keep running into layout constraint problems :/ |
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. |
i did ..and haven't gotten help |
Here's what I did in addition to @vrwim 's solution: 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! |
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. |
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 |
@bryankattah see issue #60. |
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)
The text was updated successfully, but these errors were encountered: