tableviewcell中的约束似乎被忽略了

时间:2018-07-27 02:49:25

标签: swift

我无法使这些滑块对齐,我不确定为什么。

sliders on wrong side

这是我的故事板...尾随的约束显然被忽略了。我在做什么错了?

storyboard

编辑:哑光

我删除了所有约束

和控制拖动它们中的两个,应该足够了:trailing: 14center Y(现在在超级视图中,rh被标记是错误的)。没有任何绘制的约束上的“在运行时删除”。

View debugging showing issue flags

这些标志是可见的,但是没有指示它们的含义(大小检查器无助地只说“具有不明确的约束”)。

编辑#2

根据此处的输入和快速的不一致,下面是应用了更改的视图示例:

rebuilt result 这是该情节提要的相关xml:

                                    <tableViewCell clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" restorationIdentifier="RootAppCell" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="RootAppCell" id="PDl-Cx-SrR" customClass="RootAppCell" customModule="Assignment_5">
                                    <rect key="frame" x="0.0" y="28" width="343" height="44"/>
                                    <autoresizingMask key="autoresizingMask"/>
                                    <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="PDl-Cx-SrR" id="EHS-uv-XgI" customClass="RootAppCell" customModule="Assignment_5">
                                        <rect key="frame" x="0.0" y="0.0" width="343" height="43.5"/>
                                        <autoresizingMask key="autoresizingMask"/>
                                        <subviews>
                                            <switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="uDJ-B5-x3A">
                                                <rect key="frame" x="277" y="6" width="51" height="31"/>
                                            </switch>
                                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="App Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fWL-xO-z8g">
                                                <rect key="frame" x="18" y="11" width="81" height="21"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                        </subviews>
                                        <constraints>
                                            <constraint firstItem="fWL-xO-z8g" firstAttribute="centerY" secondItem="EHS-uv-XgI" secondAttribute="centerY" id="G84-Ys-Q8G"/>
                                            <constraint firstItem="fWL-xO-z8g" firstAttribute="leading" secondItem="EHS-uv-XgI" secondAttribute="leadingMargin" constant="3" id="JrD-j0-SxV"/>
                                            <constraint firstItem="uDJ-B5-x3A" firstAttribute="centerY" secondItem="EHS-uv-XgI" secondAttribute="centerY" id="Q5Q-sy-Qh7"/>
                                            <constraint firstAttribute="trailingMargin" secondItem="uDJ-B5-x3A" secondAttribute="trailing" constant="2" id="bMt-16-diy"/>
                                            <constraint firstItem="uDJ-B5-x3A" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="fWL-xO-z8g" secondAttribute="trailing" id="he7-G6-aBc"/>
                                        </constraints>
                                    </tableViewCellContentView>

2 个答案:

答案 0 :(得分:1)

如果您不清楚哪些约束冲突,请在加载屏幕时查看控制台输出,您应该能够看到与此类似的内容

(
  "<NSLayoutConstraint:0x6199cd0 UIButton:0x7fbbf90.width == 0 (active)>",
  "<NSLayoutConstraint:0x6998ec0 H:|-(15)-[UILabel:0x6ce60] (active, names: '|':UIButton:0x7fbbf90 )>",
  "<NSLayoutConstraint:0x6199000 H:[UILabel:0x6ce60]-(15)-| (active, names: '|':UIButton:0x7fbbf90 )>"
)

为进一步帮助您,您可以使用此网页粘贴该部分,它至少会告诉您代码中哪些约束冲突。.

https://www.wtfautolayout.com/

表视图单元格可能很棘手,因为它们将基于例如您设置为单元格的行高等而受到其他约束。

希望这有助于调试

答案 1 :(得分:0)

这是我尝试过的方法,对我来说效果很好:

enter image description here

添加约束:

  1. superView的UILabel领先优势是15。
  2. UILabel的中心Y等于superView的中心Y。
  3. superView的UIView(分隔视图)的底部为0
  4. 从SuperView跟踪UIView(分隔视图)为0
  5. superView的UIView(分隔视图)的导程为0
  6. UILabel和UISwitch之间的水平距离为5。
  7. UISwitch的中心Y等于superView的中心Y。
  8. 从超级视图跟踪UISwitch是10。

UIView(除法器视图)还需要一个额外的约束,即视图的高度为1。

以下是输出:

enter image description here

希望这对您有帮助!