I get the following errors when I attempt to set a label's Preferred Width to Automatic in a storyboard:
Attribute Unavailable: Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
Since I need my layout to work on both iOS 7 and 8, I was planning to do the following:
- Set the value to Explicit in the storyboard.
- On iOS 7, set the value to an explicit, computed width programatically.
- On iOS 8, set the value to automatic programatically.
1 and 2 are easy. How do I do step 3? Is there a constant I can set it to?
Here's what I have tried so far...
If you set the value to automatic on a storyboard and you inspect preferredMaxLayoutWidth
, you will see that it is 0
.
However, attempting to set it to 0, even if it says it is already 0, doesn't work properly (e.g. the label stays as a single line). For example, I tried setting the value to automatic in the storyboard, and on viewDidLoad
, I ran the following:
self.label.preferredMaxLayoutWidth = self.label.preferredMaxLayoutWidth;
When I don't run the above code, the label is sized properly. However, when I run the above code (which should do nothing), it stays as a single line (undesirable behavior).
The header file for UILabel says:
// Support for constraint-based layout (auto layout)
// If nonzero, this is used when determining -intrinsicContentSize for multiline labels
@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);
As far as a constant, I couldn't find anything. The closest constant I can think of to what I want is UITableViewAutomaticDimension
, which doesn't work.
Here is what the storyboard looks like....
Automatic layout width:
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="1000" text="Foo" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Bis-iG-g4l">
<rect key="frame" x="20" y="116" width="560" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
Explicit layout width:
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="1000" text="Foo" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="560" translatesAutoresizingMaskIntoConstraints="NO" id="Bis-iG-g4l">
<rect key="frame" x="20" y="116" width="560" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
The only difference is that the latter has:
preferredMaxLayoutWidth="560"