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

时间:2020-02-18 13:53:34

标签: ios autolayout runtime

我有一个简单的表格视图单元格,其中包含两个标签,它们的定位约束在运行时消失(宽度和宽高比保持不变)。没有警告,情节提要视图中没有错误,没有控制台日志,并且我不使用大小类。

视图放错了位置(它们粘在左上角),当我启动Debug View Hierarchy时,紫色背景上有一个感叹号,指示位置不明确。经检查发现,位置限制不再存在。

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

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

这是情节提要的一部分,描述了表格视图控制器和原型单元:

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

1 个答案:

答案 0 :(得分:1)

好吧,那不是很明显……直到:)

以某种方式-通过“糟糕”的点击或某些Xcode / IB怪癖-您单元格的Content View的类设置为CheckListItemCell

enter image description here

选择单元格的Content View,转到Identity Inspector窗格,然后将Custom Class更改回默认的UIView

enter image description here

更好的结果:

enter image description here