xcode 需要Y位置或高度约束,但高度由标注确定

ejk8hzay  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(136)

我有一个自定义的视图嵌套栈视图和标签,我相信我有足够的约束,不会导致错误,但是,我怀疑接口建设者可能会失去标签定义的高度某种方式。无论如何,它是打破约束根据接口建设者。

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

我预计不会得到错误,但是,我尝试运行应用程序和视图出现,因为我希望/期待,但我得到了一个错误的界面构建器。

zed5wv10

zed5wv101#

当使用Xcode脚本/界面构建器设计UI时,您需要提供尽可能完整的信息。
当有歧义时,Xcode会尽最大努力 * 猜测 * 您最终会用这些视图做什么......但它无法***知道***您会做什么,所以它会尝试提供有用的注解,说明缺少了哪些信息。
在开发过程中(特别是当你刚刚开始的时候),给你的UI元素提供对比度很强的背景颜色会很有帮助,这样可以很容易地看出什么是什么。
以下是您的视图,带有颜色:

这可能不是你所期望的。界面生成器显示一个错误,因为它不知道你想要的标签如何布局。
根据您在运行时如何实现这个视图,它可能按照您想要的方式调整大小......或者,它可能按照您想要的方式调整大小 * 此时 * --但是当在它周围添加其他元素时,突然看起来不对。
几种方法来摆脱接口错误(警告)...
1.为垂直堆栈视图设置一个显式的高度-几乎可以肯定这不是您想要做的...您可能希望标签来确定高度。
1.调整标签Content Hugging Priority...例如,如果您为底部“July”标签指定252的Hugging优先级(所有标签的默认值都是251),则界面构建器将满足要求。
1.一个更好的选择是将垂直堆栈视图的分布设置为Fill Equally-在IB中它将如下所示:

现在我们已经满足了IB,所以它不再显示错误,并且布局 * 在设计时 * 更能代表它在运行时 * 的外观。
当然,和IB一样,我也在 * 猜测 * 你的布局目标......

相关问题