简体   繁体   English

需要 Y position 或高度的约束,但高度由标签决定

[英]Need constraints for Y position or height, but height is determined by labels

I have a custom view with nested stack views and labels, I believe I have enough constraints to not cause an error, however, I suspect the interface builder might be losing the label defining the height some how.我有一个带有嵌套堆栈视图和标签的自定义视图,我相信我有足够的约束不会导致错误,但是,我怀疑界面构建器可能会以某种方式丢失定义高度的 label。 Regardless, it is breaking constraints according to interface builder.无论如何,根据界面构建器,它正在打破约束。

需要至少 10 个信誉点才能发布图像

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

I expected not to get errors, however, I tried running the app and the view appeared as I was hoping/expecting, but I am getting an error in interface builder.我预计不会出现错误,但是,我尝试运行该应用程序并且视图如我所希望/期望的那样出现,但我在界面构建器中遇到错误。

When designing UI with Xcode Storyboard / Interface builder, you need to provide information as complete as possible.使用 Xcode Storyboard / Interface builder 设计 UI 时,您需要提供尽可能完整的信息。

When there are ambiguities, Xcode does its best to guess at what you will eventually do with those views... but it can't know what you will do, so it tries to provide helpful notes on what information is missing.当存在歧义时,Xcode 会尽力猜测您最终将使用这些视图做什么……但它无法知道您将做什么,因此它会尝试提供有关缺少哪些信息的有用注释。

During development (particularly when you're just starting out) it can be very helpful to give your UI elements contrasting background colors to make it easy to see what's what.在开发过程中(特别是当您刚刚开始时),为您的 UI 元素提供对比背景 colors 可以非常有帮助,这样可以很容易地看到什么是什么。

Here's your view, with colors:这是您对 colors 的看法:

在此处输入图像描述

That is probably not what you are expecting.这可能不是您所期望的。 Interface Builder shows an Error, because it doesn't know exactly how you want the labels to be laid out. Interface Builder 显示一个错误,因为它不知道您希望如何布置标签。

Depending on how you implement this view at run-time, it may be sizing just how you want... or, it may be sizing the way you want it to at the moment -- but then suddenly look wrong when other elements are added around it.根据您在运行时如何实现此视图,它可能正在按您想要的方式调整大小……或者,它可能正在按您现在想要的方式调整大小——但是当添加其他元素时突然看起来不对劲周围。

Couple ways to get rid of the Interface Error (warning)...摆脱接口错误(警告)的几种方法......

  1. Set an explicit height for the vertical stack view - almost certainly not what you want to do though... you probably want the labels to determine the height.为垂直堆栈视图设置一个明确的高度 - 几乎肯定不是你想要做的......你可能希望标签确定高度。

  2. Adjust the labels Content Hugging Priority... for example, if you give the bottom "July" label a Hugging priority of 252 (the default on all of them is 251 ), interface builder will be satisfied.调整标签 Content Hugging Priority... 例如,如果您给底部的“July”label 一个 Hugging 优先级为252 (它们的默认值都是251 ),界面生成器将得到满足。

  3. A better option might be to set the Distribution of the vertical stack view to Fill Equally - it will look like this in IB:一个更好的选择可能是将垂直堆栈视图的 Distribution 设置为Fill Equally - 在 IB 中它看起来像这样:

在此处输入图像描述

Now we've satisfied IB so it no longer shows an error, and the layout at design time is more representative of how it will look at run time .现在我们已经对 IB 感到满意,所以它不再显示错误,并且设计时的布局更能代表它在运行时的外观。

Of course, like IB, I am also guessing at your layout goal...当然和IB一样,我也在猜测你的layout目标...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 y position 或高度在堆栈视图中的堆栈视图明确时需要约束 - Need constraints for y position or height when height is unambiguous with stack views in stack views y 位置需要约束,x 位置需要约束 - need constraints for y position, need constraints for x position 在Android中定义片段布局的高度和位置 - define height and position of fragment layout in android 如果我在样式上具有特定高度的特定“ android:layout_height”,子布局是否需要特定 - If I have specific “android:layout_height” with certain height on a style, will child layout need to specific 为什么 UI Design Builder 中的“Infer Constraints”将某些视图的布局高度和宽度设置为 0dp? - Why does "Infer Constraints" in the UI Design Builder set the layout height and width of some views to 0dp? 如何在不改变位置的情况下增加 xml 中的标签高度。 viewpager 也落后于选项卡布局 - How to increase tab height in xml without changing position. Also viewpager goes behind tab layout 子视图的高度超过CoordinatorLayout的高度 - Height of a child view is exceeding height of CoordinatorLayout 根据ConstraintLayout高度设置Imageview高度 - Set Imageview Height according to ConstraintLayout height 自定义ActionBar高度和ActionBar选项卡高度 - Custom ActionBar Height and ActionBar Tab Height Android布局的高度和宽度 - Android layout Height and width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM