![](/img/trans.png)
[英]Need constraints for y position or height when height is unambiguous with stack views in stack views
[英]Need constraints for Y position or height, but height is determined by labels
我有一个带有嵌套堆栈视图和标签的自定义视图,我相信我有足够的约束不会导致错误,但是,我怀疑界面构建器可能会以某种方式丢失定义高度的 label。 无论如何,根据界面构建器,它正在打破约束。
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="CollectionsWidgetViewController" customModule="ALPaymentsPlugin">
<connections>
<outlet property="view" destination="Krl-KG-aoU" id="9m9-Vt-aGB"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="Krl-KG-aoU">
<rect key="frame" x="0.0" y="0.0" width="420" height="216"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="HoY-1F-4en">
<rect key="frame" x="5" y="5" width="410" height="206"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" ambiguous="YES" insetsLayoutMarginsFromSafeArea="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2S6-Pe-jtt">
<rect key="frame" x="0.0" y="0.0" width="410" height="185.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" insetsLayoutMarginsFromSafeArea="NO" text="Due date" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="gPa-q7-8K6">
<rect key="frame" x="0.0" y="0.0" width="205" height="185.5"/>
<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" ambiguous="YES" insetsLayoutMarginsFromSafeArea="NO" text="July 8, 2022" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="jmM-tz-kES">
<rect key="frame" x="205" y="0.0" width="205" height="185.5"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="gPa-q7-8K6" firstAttribute="width" secondItem="2S6-Pe-jtt" secondAttribute="width" multiplier="0.5" id="xvG-iK-6qr"/>
</constraints>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" ambiguous="YES" insetsLayoutMarginsFromSafeArea="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YxA-kd-JzN">
<rect key="frame" x="0.0" y="185.5" width="410" height="20.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" insetsLayoutMarginsFromSafeArea="NO" text="Due date" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fJK-U9-2XS">
<rect key="frame" x="0.0" y="0.0" width="205" height="20.5"/>
<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" ambiguous="YES" insetsLayoutMarginsFromSafeArea="NO" text="July 8, 2022" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="C1M-ND-vuD">
<rect key="frame" x="205" y="0.0" width="205" height="20.5"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="fJK-U9-2XS" firstAttribute="width" secondItem="YxA-kd-JzN" secondAttribute="width" multiplier="0.5" id="WwH-N7-QUe"/>
</constraints>
</stackView>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" systemColor="systemTealColor"/>
<constraints>
<constraint firstItem="HoY-1F-4en" firstAttribute="top" secondItem="Krl-KG-aoU" secondAttribute="top" constant="5" id="ROX-Cc-nCz"/>
<constraint firstAttribute="trailing" secondItem="HoY-1F-4en" secondAttribute="trailing" constant="5" id="uXH-Y9-O6f"/>
<constraint firstItem="HoY-1F-4en" firstAttribute="leading" secondItem="Krl-KG-aoU" secondAttribute="leading" constant="5" id="uy9-dC-D1X"/>
<constraint firstAttribute="bottom" secondItem="HoY-1F-4en" secondAttribute="bottom" constant="5" id="wTb-GO-ZEb"/>
</constraints>
<nil key="simulatedTopBarMetrics"/>
<nil key="simulatedBottomBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="149.27536231884059" y="145.98214285714286"/>
</view>
</objects>
<resources>
<systemColor name="systemTealColor">
<color red="0.18823529411764706" green="0.69019607843137254" blue="0.7803921568627451" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>
我预计不会出现错误,但是,我尝试运行该应用程序并且视图如我所希望/期望的那样出现,但我在界面构建器中遇到错误。
使用 Xcode Storyboard / Interface builder 设计 UI 时,您需要提供尽可能完整的信息。
当存在歧义时,Xcode 会尽力猜测您最终将使用这些视图做什么……但它无法知道您将做什么,因此它会尝试提供有关缺少哪些信息的有用注释。
在开发过程中(特别是当您刚刚开始时),为您的 UI 元素提供对比背景 colors 可以非常有帮助,这样可以很容易地看到什么是什么。
这是您对 colors 的看法:
这可能不是您所期望的。 Interface Builder 显示一个错误,因为它不知道您希望如何布置标签。
根据您在运行时如何实现此视图,它可能正在按您想要的方式调整大小……或者,它可能正在按您现在想要的方式调整大小——但是当添加其他元素时突然看起来不对劲周围。
摆脱接口错误(警告)的几种方法......
为垂直堆栈视图设置一个明确的高度 - 几乎肯定不是你想要做的......你可能希望标签确定高度。
调整标签 Content Hugging Priority... 例如,如果您给底部的“July”label 一个 Hugging 优先级为252
(它们的默认值都是251
),界面生成器将得到满足。
一个更好的选择可能是将垂直堆栈视图的 Distribution 设置为Fill Equally
- 在 IB 中它看起来像这样:
现在我们已经对 IB 感到满意,所以它不再显示错误,并且设计时的布局更能代表它在运行时的外观。
当然和IB一样,我也在猜测你的layout目标...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.