【问题标题】:Swift Stack View horizontal UIlabels not filling properlySwift Stack View 水平 UIlabels 没有正确填充
【发布时间】:2021-08-20 04:09:26
【问题描述】:

所以我正在设计应该是一个简单的视图。

这就是我想要的样子,标题只占用它需要的水平空间。但是,当我将 Details 视图的行数设置为 0 以便它可以是多行时,或者当我对标题标签执行相同操作时,我会自动得到:

我确实喜欢为这些标签使用堆栈视图,因为它似乎是考虑动态文本的最自然选择。当文本变大时,我要做的就是将轴更改为垂直。我已经将标题标签的拥抱优先级设置为 252,并且我已经设置了比例宽度约束,以便细节将具有大于或等于标题的宽度。

所以标签的宽度没有歧义

  • 标题标签的宽度应等于其内容的宽度,直到内容达到超出详细信息标签宽度的程度,然后自动换行。

  • 详细信息标签的宽度也应等于其内容,直到它超过左侧的较高优先级拥抱和右侧的尾随约束所允许的范围,然后它应该自动换行。

这是我的 xib 作为 xml 源代码

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="18122" 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="18093"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <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"/>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view contentMode="scaleToFill" id="iN0-l3-epB">
            <rect key="frame" x="0.0" y="0.0" width="372" height="76"/>
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <stackView opaque="NO" contentMode="scaleToFill" spacing="5" translatesAutoresizingMaskIntoConstraints="NO" id="pIM-2q-cOU">
                    <rect key="frame" x="0.0" y="0.0" width="372" height="76"/>
                    <subviews>
                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="252" verticalHuggingPriority="251" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="G5U-AG-1Se">
                            <rect key="frame" x="0.0" y="0.0" width="33" height="76"/>
                            <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="asasdf;lkj;lkjasdf" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pjO-IR-TuP">
                            <rect key="frame" x="38" y="0.0" width="334" height="76"/>
                            <fontDescription key="fontDescription" type="system" pointSize="17"/>
                            <nil key="textColor"/>
                            <nil key="highlightedColor"/>
                        </label>
                    </subviews>
                </stackView>
            </subviews>
            <viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
            <color key="backgroundColor" systemColor="systemBackgroundColor"/>
            <constraints>
                <constraint firstAttribute="bottom" secondItem="pIM-2q-cOU" secondAttribute="bottom" id="1y4-EF-89I"/>
                <constraint firstItem="pIM-2q-cOU" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="6n3-Qg-845"/>
                <constraint firstItem="pIM-2q-cOU" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="Ey8-Pq-GRr"/>
                <constraint firstAttribute="trailing" secondItem="pIM-2q-cOU" secondAttribute="trailing" id="O6p-XR-VEs"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="pIM-2q-cOU" secondAttribute="trailing" id="Z5d-jc-cwM"/>
            </constraints>
            <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
            <variation key="default">
                <mask key="constraints">
                    <exclude reference="O6p-XR-VEs"/>
                </mask>
            </variation>
            <point key="canvasLocation" x="-16" y="-101"/>
        </view>
    </objects>
    <resources>
        <systemColor name="systemBackgroundColor">
            <color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
        </systemColor>
    </resources>
</document>

【问题讨论】:

  • 如果您将拥抱优先级设置得更高(例如 1000),它应该可以工作。也就是说,当两个标签都有大量文本时,您希望发生什么?是否希望每个标签占据可用宽度的一半?
  • "当两个标签都有大量文本时,您希望发生什么?您希望每个标签都占据可用宽度的一半吗?"是的。
  • 似乎将拥抱优先级设置得更高可能起到了作用。你能解释一下为什么吗?
  • 我不知道,但似乎只有 1000 个(“必需”)有效。也许还有其他东西(可能是堆栈视图所做的事情)具有 1000 优先级,可以防止标签拥抱内容。
  • 好吧,如果您想将其添加为答案,我同意。

标签: swift xml uikit xib


【解决方案1】:

如果将左侧标签的内容拥抱优先级设置为1000(必填),则可以。

堆栈视图中可能有一些具有非常高优先级的东西阻止标签拥抱其内容。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多