2

A UIImageView is added to a UITableViewCell using Auto-Layout.

The width of the UIImageView should be 50% of the width of the UITableViewCell but in any case not larger than 50px.

This does not work:

    NSLayoutConstraint.activate([
        cellImageView.topAnchor.constraint(equalTo: contentView.topAnchor),
        cellImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        cellImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
        cellImageView.heightAnchor.constraint(equalTo: cellImageView.widthAnchor, multiplier: cellImage.size.height / cellImage.size.width),
        cellImageView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.5),
        cellImageView.widthAnchor.constraint(lessThanOrEqualToConstant: 50)
        ])

How do I have to define the layout constraints?

2 Answers 2

3

Is this a layout library? I'm not familiar with the syntax in your code, but based on your description, it sounds like the width constraints you want are:

  • lessThanOrEqualToConstant: 50
  • equalTo: contentView.widthAnchor, multiplier: 0.5 with a lower priority than the 50pt constraint.
Sign up to request clarification or add additional context in comments.

5 Comments

Why would it be greaterThanOrEqualTo if the image width should be 50% of the cell width or 50px maximum? The syntax is Swift 3.
Ah, well your edit just reversed that. I will edit my answer. The key part is the lower priority on the second constraint.
Sorry, my description was maybe not clear enough, I meant it to be max 50% or smaller. Updated the post :)
No problem. Let me know if setting the lower priority solves the issue.
Thanks, it worked by setting the 50% constraint to 999.
1

You would set up two separate constraints with different priorities. One constraint would be 50% width with a UILayoutPriority.DefaultHigh priority (e.g. 750) while the second constraint would be a 50 point constraint with UILayoutPriority.Required priority (e.g. 1000). This would then attempt to fulfill both constraints, but would break the 50% one if they ran into conflict (i.e. went over 50 points).

3 Comments

I'm confused, you said something different above ("it worked by setting the 50% constraint to 999"). This comment is a bit confusing too, since this would infer that the 50% is stronger than the 50pt constraint. Which would keep the view at 50% since that is the strongest constraint.
Sorry, here the corrected comment: "This didn't work because when I set UILayoutPriority.DefaultHigh for the 50% constraint it would not come into effect at all, even without the 50pt constraint activated. The solution was to leave the priority of the 50pt constraint (which is 1000 by default) and set the 50% priority to 999." So your solution worked almost :)
Ah, that makes more sense. Thanks for the clarification. Yeah, sometimes high is not enough, given other [sometimes more hidden] active constraints (e.g. hugging/stretching). Glad 999 worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.