自动布局约束在运行时消失

Autolayout constraint disappearing at runtime

我有一个简单的 table 视图单元格,其中包含两个标签,其定位约束在运行时消失(宽度和纵横比保持不变)。没有警告,故事板视图中没有错误,没有控制台日志,而且我没有使用大小 类。

视图错位(它们粘在左上角),当我启动调试视图层次结构时,紫色背景上有一个感叹号,表示位置不明确。经检查发现位置限制已经不存在了。

我已经尝试再次删除和添加它们,我已经将视图层次结构中的几乎每个 IB 检查器与工作实现进行了比较,但无法找出区别。我用 lldb 检查了视图,_autolayoutTrace 没有显示约束。

我知道我可以在 Interface Builder 中删除控制器并重建它,但我希望能深入了解调试此类问题。

这是情节提要中描述 table 视图控制器和原型单元格的部分:

<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>

好吧,这并不明显......直到它是:)

不知何故 - 通过 "oops" 点击或一些 Xcode / IB 怪癖 - 你的单元格 Content View 将其 class 设置为 CheckListItemCell:

Select 单元格的 Content View,转到身份检查器窗格,并将 Custom Class 更改回默认值 UIView:

更好的结果: