I have a problem with iOS 8 autolayout
.
I want to have a custom UITableviewCell
with a UILabel
and 5 UIImageView
. I want the images to be shown in landscape-mode but not in portrait mode, because then the UILabel
would be squeezed to narrow.
I designed the view in interface builder and added the following constraints (among others):
1. priority 1000: Label width >= 120px
2. priority 1000: ImageView[1..5]
horizontal space to next one = 0px (to align them next to each
other, the rightmost image view got horizontal space = 0 to content
view to align them to the right side)
3. priority 999: ImageView[1..5]
width = 40px
4. priority 998: ImageView[1..5] width = 0px (hide image
views if the 40px constraint can't be satisfied because of the
required label width)
I also set ImageView
to center the image and to clip subviews, as well as content compression priority to 1 (lowest).
The UILabel
content hugging priority I also set to 1 so that it even resizes if the text is shorter than 120px.
What happens now is:
- landscape mode:
everything works fine
| Labelxxxxx img1 img2 img3 img4 img5 |
- portrait mode:
image views get squeezed to something like 10px (which means that it breaks constraint #3 (40px width) in order to satisfy constraint #1 (label width), but it does not set the image views widths to 0px which would satisfy constraint #4)
| Labelxxxxx im.im.im.im.im. |
I don't get any exception or log message about any unsatisfiable constraints (not even for the 40px constraint although the image views get squeezed to 10px)
Thanks in advance for any help you can provide!
--edit--
I tried some alternatives, including setting the constrains programmatically, but it looks like they are also set as intended when using IB.
Shouldn't it be possible to set 2 contradictory constraints with different priorities - as soon as the one with the higher priority needs to be broken to the advantage of an even higher constraint the second one should be used as as fail-over?
(In my case: label width = highest priority, breaks ImageView
width 40px constraint --> ImageView
width 0px as fail-over)
I had similar issue but solved by following method
Added one DummyView inside UITableViewCell and Pin all side of DummyView to UITableViewCell .
Now add all Image , Label and other subiview inside DummyView.
Create IBOutLet for DummyView and subView you added.
Programmatically remove all constraints
Programmatically add all constraints based on device orientation.
I'd probably wire IBOutlets to the width constraints and change the widths to 0 on rotation.