Reputation: 3603
I have a UIScrollView
with a big UITextView
in it. I created a constraint to make the text view width 100%, I now would like the textview to resize in height when orientation changes and I can't find how to do this.
Here's my code for now:
var wordLabel = UITextView()
wordLabel.attributedText = wordText
wordLabel.editable = false
wordLabel.scrollEnabled = false
wordLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
let wordLabelSize = wordLabel.sizeThatFits(maxLabelsSize)
wordLabel.frame = CGRectMake(0, 0, wordLabelSize.width, wordLabelSize.height)
wordLabel.center = CGPoint(x: wordLabel.frame.width / 2, y: wordLabel.frame.height / 2)
scrollView.addSubview(wordLabel)
let wordLabelConstraintWidth = NSLayoutConstraint(item: wordLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: -32)
scrollView.addConstraint(wordLabelConstraintWidth)
scrollView.contentSize = CGSize(width: scrollView.frame.width - 32, height: wordLabel.frame.origin.y + wordLabel.frame.height)
scrollView
and wordLabel
sizes are correct when the app launches but when orientation changes to landscape, the scroll gets too high.
I don't really know how to recalculate scrollView
contentSize on orientation change, any idea?
Upvotes: 0
Views: 911
Reputation: 17707
You have two way:
Set a constraint for the height, and do something like:
wordLabelConstraintHeight.costant = (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) ? 60.0 : 25.0;
If the textView
should resize itself proportionally with the screen, you can use the multiplier:
let wordLabelConstraintHeight = NSLayoutConstraint(item: wordLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Height, multiplier: 0.2, constant: 0)
For example this code make the height of your textView
, the 20% of the frame of your scrollView
that I'm sure is changing according with the screen size and so according with the orientation of the interface.
Upvotes: 1