Swift 堆栈视图水平 UI 标签未正确填充
Swift Stack View horizontal UIlabels not filling properly
所以我设计的应该是一个简单的视图。
这就是我想要的样子,标题只占据它需要的水平 space。但是,当我将“详细信息”视图的行数设置为 0 以便它可以是多行时,或者当我对标题标签执行相同操作时,我会自动得到:
我确实喜欢为这些标签使用 Stack View,因为它似乎是解释动态文本的最自然选择。当文本变大时,我要做的就是将轴更改为垂直。我已经将标题标签的拥抱优先级设置为 252,并且我已经设置了比例宽度约束,以便细节的宽度大于或等于标题。
所以标签的宽度没有歧义
标题标签的宽度应等于其内容的宽度,直到内容超出详情标签的宽度,然后自动换行。
details label 的宽度也应该和它的内容一样,直到它超过左边更高优先级的 hugging 和右边的 trailing constraint 所允许的边界,然后它应该自动换行。
这是我的 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(必需),它会起作用。
堆栈视图中可能有一些优先级很高的东西正在阻止标签包含其内容。
所以我设计的应该是一个简单的视图。
这就是我想要的样子,标题只占据它需要的水平 space。但是,当我将“详细信息”视图的行数设置为 0 以便它可以是多行时,或者当我对标题标签执行相同操作时,我会自动得到:
我确实喜欢为这些标签使用 Stack View,因为它似乎是解释动态文本的最自然选择。当文本变大时,我要做的就是将轴更改为垂直。我已经将标题标签的拥抱优先级设置为 252,并且我已经设置了比例宽度约束,以便细节的宽度大于或等于标题。
所以标签的宽度没有歧义
标题标签的宽度应等于其内容的宽度,直到内容超出详情标签的宽度,然后自动换行。
details label 的宽度也应该和它的内容一样,直到它超过左边更高优先级的 hugging 和右边的 trailing constraint 所允许的边界,然后它应该自动换行。
这是我的 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(必需),它会起作用。
堆栈视图中可能有一些优先级很高的东西正在阻止标签包含其内容。