How to match labelwidth in scrollview with screenw

2019-09-07 03:21发布

问题:

I recently started using autolayout in my apps and reached a point, where I just don't seem to find an answer.

The case is simple enough:

I have a ViewController with a single scrollview inside of it. The scrollview should take up all space in both landscape and portrait mode. Within this scrollview I want to have a label, that takes enough space to show its content, but only to a maximum of the current scrollview-width (which should be no more than the screen size). the remaining text should just be clipped.

So basically I don't want the scrollview to scroll horizontally.

If possible, I prefer a solution that uses only interface builder.

回答1:

In regard to your comment: this situation happens, because the size of a scroll view is dependent on its content (see the section Pure Auto Layout Approach). So, the size of a label cannot depend on the size of the scroll view.

There's a workaround to this - you can set the constraints between the label and the main view, e.g.:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label
                                                      attribute:NSLayoutAttributeLeft
                                                      relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeLeft
                                                     multiplier:1.0
                                                       constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label
                                                      attribute:NSLayoutAttributeRight
                                                      relatedBy:NSLayoutRelationLessThanOrEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeRight
                                                     multiplier:1.0
                                                       constant:-40]];

With this approach there'll be at least 40 points of distance between the left (right) side of the label and the left (right) side of the scroll view (as the scroll view will fill the main view).

Alternatively, you can set up a constraint between the label's and the main view's width's. It'd be something like this:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationLessThanOrEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:1.0
                                                       constant:-40]];

So right now the label's width won't be larger than the main view's width - 40.