Swift Stack View 水平 UIlabels 未正确填充 [英] Swift Stack View horizontal UIlabels not filling properly

查看:22
本文介绍了Swift Stack View 水平 UIlabels 未正确填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在设计一个简单的视图.

So I am designing what should be a simple view.

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

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:

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

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.

所以标签的宽度没有歧义

  • 标题标签的宽度应该等于其内容的宽度,直到内容超过细节标签的宽度,然后自动换行.

  • the title label width should equal the width of its contents, until the contents reach the point that they would exceed the width of the details label, then word wrap.

详细信息标签的宽度也应该与其内容相同,直到超出左侧更高优先级的拥抱和右侧的尾随约束所允许的边界,然后它应该自动换行.

The details label should should have a width equal to its contents as well, until it would exceed the bounds allowed by the higher priority hugging on the left and the trailing constraint on the right, then it should word wrap.

这是我的xib作为xml源代码

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>

推荐答案

如果将左标签的内容拥抱优先级设置为 1000(必需),则有效.

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

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

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

这篇关于Swift Stack View 水平 UIlabels 未正确填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆