I am using autolayout within a UICollectionViewCell. Super simple layout: just a UILabel. I want the UILabel to take up the full width minus a 20 px inset and be centered in the cell both vertically and horizontally. I have set up constraints that do just that. If i run it on any ios 8 device or simulator it works perfectly. However, when I run it on some ios 7 devices the constraints have no effect. I tried looking through the apple docs, but none of their changes seems to be around auto layout.
Here's the XML source code though I doubt it means much:
<constraints>
<constraint firstItem="OIc-cg-9hO" firstAttribute="leading" secondItem="Ga6-nx-lOn" secondAttribute="leading" constant="20" id="A7U-sd-fcL"/>
<constraint firstAttribute="centerY" secondItem="OIc-cg-9hO" secondAttribute="centerY" id="G9e-9W-aDS"/>
<constraint firstAttribute="centerX" secondItem="OIc-cg-9hO" secondAttribute="centerX" id="TrB-hI-7Kw"/>
<constraint firstAttribute="trailing" secondItem="OIc-cg-9hO" secondAttribute="trailing" constant="20" id="yjH-nf-D9U"/>
</constraints>
More of a workaround than an answer: but I added the constraints in code as follows:
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:-20.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
In order for it to work I needed both the coded constraints and the IB constraints. Dunno Why!
It sounds like your Interface Builder constraints are using the "Constraint to Margin" option which is not supported on iOS7. Open the constraints in IB and check if any of them have items with the 'relative to margin' option checked.
See What is "Constrain to margin" in Storyboard in Xcode 6 and What is "Constrain to margin" in Storyboard in Xcode 6 for more details on constraint to margin.
This happened to me, you have to check every constraint in the UI and remove the Relative to Margin which was introduced with iOS 8 and doesn't support iOS 7, don't forget those inside some places where you might forget (like UITableView cells).
To find this option:
Tap on the UI control
Go to the Size Inspector
Double click any constraint and check the margin thing, just turn it off and fix your constraint if anything changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With