mardi 28 juin 2016

How to get UIView that contains UITextView to resize based on content Swift


I am trying to create a messaging app which has the same method of sending messages that iMessage does. I put a UITextView in a UIView. I have been able to get the UITextView to resize when text reaches its width. The UITextView then grows in height with each new line, however, the UIView that contains this UITextView is not scaling along with it I used code from another stack post. i was unable to ask a question on that thread. The problem is that I can't get the actual parent UIView to scale properly Here is the code: func textViewDidChange(textView: UITextView) { print("text view did changen") let textViewFixedWidth: CGFloat = self.textView.frame.size.width let newSize: CGSize = self.textView.sizeThatFits(CGSizeMake(textViewFixedWidth, CGFloat(MAXFLOAT))) var newFrame: CGRect = self.textView.frame // var textViewYPosition = self.textView.frame.origin.y var heightDifference = self.textView.frame.height - newSize.height // if (abs(heightDifference) > 5) { newFrame.size = CGSizeMake(fmax(newSize.width, textViewFixedWidth), newSize.height) newFrame.offsetInPlace(dx: 0.0, dy: heightDifference) // //updateParentView(heightDifference) } self.textView.frame = newFrame } func updateParentView(heightDifference: CGFloat) { // var newContainerViewFrame: CGRect = self.viewContainer.frame // var containerViewHeight = self.viewContainer.frame.size.height print("container view height: (containerViewHeight)n") // var newContainerViewHeight = containerViewHeight - heightDifference print("new container view height: (newContainerViewHeight)n") // var containerViewHeightDifference = containerViewHeight - newContainerViewHeight print("container view height difference: (containerViewHeightDifference)n") // newContainerViewFrame.size = CGSizeMake(self.viewContainer.frame.size.width, newContainerViewHeight) // newContainerViewFrame.offsetInPlace(dx: 0.0, dy: containerViewHeightDifference) // self.viewContainer.frame.size.height = self.textView.frame.size.height } With updateParentView(heightDifference) commented out: enter image description here With updateParentView(heightDifference) NOT commented out: enter image description here

Aucun commentaire:

Enregistrer un commentaire