0

Having a strange issue using Auto Layout with a UILabel where it resizes almost to its content size less one line (driving me nuts). So basically, it seems to get to where it should minus a few pixels causing an ellipses at the end when downsizing to the iPhone 5 or 6. It works fine on the 6+, which is what the XIB is sized for.

Here's what the XIB looks like and what I have the constraints set to. The label is the "Big long label here is the event description".

enter image description here

Based upon this stack post, I thought I was going to figure it out. I tried both methods mentioned in the post. All four of the constraints in the screenshot are set to 1,000. If I do what the post says and add a height constraint with priority of 500, which is less than the vertical hugging and vertical compression resistance priorities, it still doesn't work. I've also just not set the height constraint, kept the preferred width set and had leading and trailing space constraints set, which was mentioned in the comments. The number of lines is set to 0.

To try to give more detail, the UILabel is in a UIView set as contentView. I then add the contentView to a UIScrollView. The UIScrollView is being automatically sized by pinning the contentView at 0 to all four sides. The contentContainer is also constrained to the screen width (not ideal but only way I've been able to get it to work - not sure if it has anything to do with this issue). I'm not getting any constraint issues in the console. translatesAutoresizingMaskIntoContraints is set to NO for the contentView. Here's what the UILabel looks like when ran:

enter image description here

It should be one line longer at the bottom to fit. Someone please help me!

Community
  • 1
  • 1
cgood
  • 155
  • 1
  • 1
  • 16

1 Answers1

3

I think I might have solved this, but the solution seems hacky. It seemed to be an issue of setting the preferredMaxLayoutWidth. It worked fine for what I had set for the iPhone 6+, but for some reason almost got there with significantly less width on the iPhone 5/6. Just added this:

self.eventDescriptionLabel.preferredMaxLayoutWidth = self.eventDescriptionLabel.frame.size.width;

And now it works fine.

cgood
  • 155
  • 1
  • 1
  • 16
  • 1
    Yes, that is the correct way to fix your problem and yes it is hacky. Apple really should do something about that imho because it throws a lot of people off. If you subclass UILabel, place this code in there, and then use your subclass instead of UILabel directly then you will save yourself a lot of headaches. – xsee Oct 24 '14 at 20:55