Reputation: 1966
I am having a horrible time trying to fix the width of a textview in autolayout. The textview is the dominant element in the screen so my inability to fix it is wrecking the entire screen.
The set up is a view containing a scrollview, containing a content view containing elements. Following Apple's instructions, I set the width of the view, scrollview and content view to 375 and pinned the contentview to the scrollview and scrollview to the view on all four sides, all with constants of 0.
I would like the textview to be centered and fairly wide. Accordingly, I first set it with a leading constant to contentview of 40, a trailing constant to contentview of 40, and a width of 295. On a 7 Plus, this left too wide a margin at the right and on a SE, the textview extended beyond the right margin. The natural thing seemed to be to remove the width constraint so there was only a leading and trailing constraint but this caused the entire view to shrink town to 113. There is no discernible reason I can come up with for this to happen, but happen it did. I then tried resetting to 295 but less than or equal. I also tried setting to 295 but lowering the priority. At priority 750, it acted as though at 1000. At priority 500, the view shrank back down to 113.
Most exasperating. Attached see screenshots of the constraints. Thanks in advance for any suggestions.
Edit:
It appears from this post that UITextViews may not adjust their size in response to autolayout. So perhaps there is no way to do this all in autolayout instoryboard with wiring a constraint and modifying it with code. I can't believe that Apple does not support autolayout with textviews in story board so still hoping there is a better way.
Upvotes: 0
Views: 348
Reputation: 100523
Control drag from the content view to the outmost view and make them Equal Widths
Upvotes: 1
Reputation: 4657
If I understand you correctly you are setting the width of your view/scrollView/contentView to a fixed width, i.e. 375 pt. This would obviously give you problems on smaller screens (like iPhone 5) and larger ones (like Plus).
You should in stead set the content view to have equal width as your outermost view.
If you are working with a storyboard and your outermost view is the main view of a view-controller then its width is already set for you are good to go. If in stead you have a xib then the width of the view should be set where you are adding it as a subview. Obviously its width should be set relative to the screen width.
Upvotes: 0