How should I setup auto layout constrains so that multiline label stays vertically centered inside scrollview until it's text content becomes too long to be shown at once? When the text length becomes too long text should be aligned at top with the scrollview so the user can see the beginning of text and scroll for more. This is how I tried to setup constrains
for scrollView:
- Equal Height to: Superview
- Align Trailing to: Safe Area, Equals = -8
- Align Leading to: Safe Area, Equals = 8
- Align Top to: Safe Area
for label:
- Leading Space to: Superview
- Equal Width to: Superview
- Align Center Y to: Superview
I also added following code to viewDidLoad()
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
The problem is that but I still get some warnings and also text ends up "vertically centered" inside scrollview without possibility to really scroll to the beginning or the end of it, even when it can not fit whole inside. For scrollview I get warning that it "has ambiguous scrollable content width", while for the label I get warning "trailing constraint is missing, which may cause overlapping with other views"
UITextView? You can set theUITextViewto infer its height from its content and it will become scrollable once it reaches a particular size based on your constraints. - Rikh