ScottyBlades
ScottyBlades

Reputation: 13963

Swift Stack View horizontal UIlabels not filling properly

So I am designing what should be a simple view.

enter image description here

This is how I want it to be, with the title taking up only the horizontal space that it needs. However, when I set the number of lines for the Details view to 0 so that it can be multiple lines, or when I do the same for the title label, I automatically get this:

enter image description here

I do like using a Stack View for these labels, because it seems the most natural choice to account for dynamic text. All that I would have to do when the text gets larger is change the axis to vertical. I have already set the hugging priority of the title label to 252 and I have already set a proportionate widths constraint so that the details will have a greater or equal width to the title.

So there is no ambiguity for the widths of the labels

Here is my xib as an xml source code

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

Upvotes: 1

Views: 1459

Answers (1)

Sweeper
Sweeper

Reputation: 270758

If you set the content hugging priority of the left label to 1000 (Required), it works.

enter image description here

enter image description here

There might be something with a very high priority in the stack view that is stopping the label from hugging its content.

Upvotes: 3

Related Questions