5

I've created a simple view setup in interface builder. Here's how it looks:

IB View Layout

The view hierarchy is simply:

view
- scrollView
-- label

The scroll view is shown with grey background anchored to its super view top, leading, trailing, and bottom with constraints of 0.

The label is shown with yellow background and has constraints as shown. Additionally, the label has content hugging priority of 1000 for both horizontal and vertical, and it has content compression resistance priority of 1000 for both horizontal and vertical.

In portrait orientation, the label is sized correctly:

Portrait Orientation Screenshot

In landscape orientation, however, the label is not sized correctly horizontally (I intended for the label to fill the width of the screen, less constraint insets as shown):

Landscape Orientation Screenshot

How can I get this label to size correctly horizontally in landscape orientation?

JRG-Developer
  • 12,454
  • 8
  • 55
  • 81

2 Answers2

14

There is one solution for you.

1. Add your UIScrollView to container (UIView) with zero constraints: enter image description here

enter image description here

2. Add constraints for Label: top, bottom, leading, trailing spaces = 20.

3. Add constraint: label.width = container.width - 40

  • For this, select label in the view structure tree, tap ctrl and pull to container. And select Equal Widths.

enter image description here

  • Then select the created constraint and go to its Utilities and set the constant value to 40.

enter image description here

You should get the following components: enter image description here

Run the app, go to landscape and it works!

enter image description here

Hope it is clear. Best Regards.

SoVa_
  • 379
  • 4
  • 12
  • Thanks alot! It's the best solution on stack! – Evgeny Fedin Jun 10 '15 at 10:28
  • How i should set constrants if i got TableView under Label (inside same ScrollView)? – Evgeny Fedin Jun 10 '15 at 18:01
  • @EvgenyFedin, as I understand you want to add some TableView at the bottom of ScrollView that the table view will be whole viewed without inside scrolling. – SoVa_ Jul 16 '15 at 11:01
  • @EvgenyFedin, For example, you can add tableView on ScrollView, which will be expanded. Then just to add leading, trailing, bottom constraints to superview and top constraint to label. Also you should add the constraint to height of tableView and connect it with outlet in controller. Then update height constraint after each reloading table view data: tableHeightConstraint.constant = tableView.contentSize.height. So the the scrollView will be so big as it required. – SoVa_ Jul 16 '15 at 11:12
-3

It's hard to tell which constraint(s) to add/remove in order to get what you want because iOS reserve the right to adjust your constraints whenever it becomes impossible for it to satisfy all your constraints.

I make a blank project with the same view you have (UILabel as subview of UIScrollView) and make some constraints to get the UILabel resized properly on landscape.

enter image description here

A must check though:

  • Make sure you set the vertical/horizontal spacing constraints from the pin option, as shown below and try to remove unneeded constraints manually.

enter image description here

Malloc
  • 15,434
  • 34
  • 105
  • 192
  • Thanks for the response. Can you elaborate on "make some constraints to get the `UILabel` resized properly" ? – JRG-Developer May 12 '14 at 22:36
  • @JRG-Developer Add horizontal center constraint for your label it will work – Raj Subbiah May 13 '14 at 06:58
  • First select the scrollview then the PIN option on the bottom and click on the right and left spacing so they appear on red (also set their values to 20 for your example). Do the same for the `UILabel`. Also, if you are using Xcode5, you may benefit from the automatic `Resolve Auto Layout Issues` option, select it and then select `Add missing constraints` from the popup menu. – Malloc May 13 '14 at 09:50