Tel Tel - 4 months ago 7
Swift Question

How to correctly resize in Swift a textView when it has to fit all the screen?

enter image description here

I have a storyboard with two controls: a textView and a tableView. In some circumstances I move the tableView out of the screen and I resize the textView in a way that fits all the screen. The trailing of the textView (which is the element at the left side) equals the left of the tableView (which is the element at the right side) - 10.

When I hide the tableView of the screen I use this code to resize the textView:

let tableViewFrame = self.tableView.frame
self.mainTextFieldTrailingConstraint.constant = mainTextFieldTrailingConstraint.constant + tableViewFrame.width


When I show the tableView, I use this code to resize the textView:

let tableViewFrame = self.tableView.frame
self.mainTextFieldTrailingConstraint.constant = self.mainTextFieldTrailingConstraint.constant - tableViewFrame.width


All seems work fine when I run the code in portrait orientation but, if then I rotate the device in landscape orientation, the right edge of the field exceeds the right side of the screen. What am I doing wrong?

Answer

when hiding tableview you need set size of textfield to full size of current view.

 self.mainTextFieldWidthConstraint.constant = self.view.frame.size.width
Comments