Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoLayout works in ios 8 but not in ios 7?

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!

like image 935
user3534641 Avatar asked Oct 03 '14 00:10

user3534641


2 Answers

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.

like image 142
pposthoorn Avatar answered Oct 03 '22 22:10

pposthoorn


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:

  1. Tap on the UI control

  2. Go to the Size Inspector

  3. Double click any constraint and check the margin thing, just turn it off and fix your constraint if anything changes.

enter image description here

like image 38
Boda Avatar answered Oct 03 '22 20:10

Boda