Gael
Gael

Reputation: 459

Autolayout constraint disappearing at runtime

I have a simple table view cell containing two labels whose positioning constraints disappear at runtime (the width and aspect ration remain). There are no warnings, no errors in the storyboard view, no console log and I don't use size classes.

The views are misplaced (they stick to the top left corner) and when I launch the Debug View Hierarchy there is an exclamation mark on a purple background indicating an ambiguous position. Upon inspection it turns out that the position constraints are not there anymore.

I have tried removing and adding them again, I have compared almost every IB inspector in the view hierarchy with a working implementation and couldn't figure out a difference. I have inspected the views with lldb and the _autolayoutTrace doesn't show the constraints.

I know I can remove the controller in Interface Builder and rebuild it but I would appreciate an insight into debugging that kind of issue.

Here is the part of the storyboard describing the table view controller and prototype cell :

<tableViewController id="RTg-bl-d8Z" customClass="ChekListViewController" customModule="Todo_app" customModuleProvider="target" sceneMemberID="viewController">
    <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="5dF-Vx-S1N">
        <rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
        <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
        <prototypes>
            <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="detailDisclosureButton" indentationWidth="10" reuseIdentifier="checkListItem" id="a6e-eR-CO4" customClass="CheckListItemCell" customModule="Todo_app" customModuleProvider="target">
                <rect key="frame" x="0.0" y="28" width="414" height="57.5"/>
                <autoresizingMask key="autoresizingMask"/>
                <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="a6e-eR-CO4" id="EUI-T4-UFi" customClass="CheckListItemCell" customModule="Todo_app" customModuleProvider="target">
                    <rect key="frame" x="0.0" y="0.0" width="351" height="57.5"/>
                    <autoresizingMask key="autoresizingMask"/>
                    <subviews>
                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="✔️" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="KM3-zM-YXK">
                            <rect key="frame" x="28" y="11" width="25.5" height="35.5"/>
                            <constraints>
                                <constraint firstAttribute="width" constant="25" id="uyC-B0-CMt"/>
                                <constraint firstAttribute="width" secondItem="KM3-zM-YXK" secondAttribute="height" multiplier="25:35" id="yPH-cC-BhV"/>
                            </constraints>
                            <fontDescription key="fontDescription" type="system" pointSize="17"/>
                            <nil key="textColor"/>
                            <nil key="highlightedColor"/>
                        </label>
                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="I1F-jX-unK">
                            <rect key="frame" x="61.5" y="18.5" width="281.5" height="21"/>
                            <fontDescription key="fontDescription" type="system" pointSize="17"/>
                            <nil key="textColor"/>
                            <nil key="highlightedColor"/>
                        </label>
                    </subviews>
                    <constraints>
                        <constraint firstAttribute="trailingMargin" secondItem="I1F-jX-unK" secondAttribute="trailing" id="2DJ-vu-eZH"/>
                        <constraint firstItem="I1F-jX-unK" firstAttribute="centerY" secondItem="KM3-zM-YXK" secondAttribute="centerY" id="5sk-xI-Lws"/>
                        <constraint firstItem="KM3-zM-YXK" firstAttribute="leading" secondItem="EUI-T4-UFi" secondAttribute="leadingMargin" constant="8" id="T2a-1U-YX9"/>
                        <constraint firstItem="I1F-jX-unK" firstAttribute="leading" secondItem="KM3-zM-YXK" secondAttribute="trailing" constant="8" id="a2G-6H-iVm"/>
                        <constraint firstItem="KM3-zM-YXK" firstAttribute="bottom" secondItem="EUI-T4-UFi" secondAttribute="bottomMargin" id="c6K-zY-2tu"/>
                        <constraint firstItem="KM3-zM-YXK" firstAttribute="top" secondItem="EUI-T4-UFi" secondAttribute="topMargin" id="z4V-Rz-c6k"/>
                    </constraints>
                </tableViewCellContentView>
                <connections>
                    <outlet property="checkMark" destination="KM3-zM-YXK" id="qY4-JT-7iv"/>
                    <outlet property="textItem" destination="I1F-jX-unK" id="eID-MO-fVV"/>
                    <segue destination="Kuh-LZ-DVA" kind="presentation" identifier="editItem" trigger="accessoryAction" id="DeL-Ql-cQq"/>
                </connections>
            </tableViewCell>
        </prototypes>
        <sections/>
        <connections>
            <outlet property="dataSource" destination="RTg-bl-d8Z" id="34r-cH-2bg"/>
            <outlet property="delegate" destination="RTg-bl-d8Z" id="CsE-gc-hUN"/>
        </connections>
    </tableView>
    <navigationItem key="navigationItem" title="Name of the list" id="05Z-xs-cMG">
        <barButtonItem key="rightBarButtonItem" systemItem="add" id="oSl-fh-Gh3">
            <connections>
                <segue destination="Kuh-LZ-DVA" kind="presentation" identifier="addItem" id="6cn-yq-cSG"/>
            </connections>
        </barButtonItem>
    </navigationItem>
</tableViewController>

Upvotes: 1

Views: 204

Answers (1)

DonMag
DonMag

Reputation: 77647

OK, that was not obvious... until it was :)

Somehow - either by an "oops" click or some Xcode / IB quirk - your cell's Content View got its class set to CheckListItemCell:

enter image description here

Select the cell's Content View, go to the Identity Inspector pane, and change the Custom Class back to the default UIView:

enter image description here

Better result:

enter image description here

Upvotes: 2

Related Questions