标签旁边有可变长度的标签

问题描述 投票:0回答:2

我想有tableview的行,其中包含两个标签。 First将包含一个名称,该名称可以是不同的长度,第二个将包含括号中的数字。例如 :

|示例名称(8)| |示例非常长的名称(10)| |非常非常非常......(21)| |姓名(6)| |示例名称名称n ...(2)|

带点的行应该是可滚动的。第二个标签应始终位于第一个标签旁边但如果第一个标签太长,则第二个标签仍应在屏幕上可见,并且首先应该是水平可滚动的。

使用DonMag的解决方案:

它几乎可以工作。我得到了我想要的视图但是左边的标签不可滚动。它最后有点,我看不到标签的其余部分。

我的桌面视图:

enter image description here

约束:

enter image description here

正确的标签:

enter image description here

滚动视图:

enter image description here

ios
2个回答
0
投票

有点棘手,但不是太棘手......

你需要做的是设置多个相似的约束,并在不同的元素上使用Content Compression Resistance PriorityConstraint Priority的组合。

看看这个:

enter image description here

  • 我们在滚动视图中嵌入了Left Label。
  • 我们将滚动视图的宽度限制为标签的宽度,但优先级为750。这将允许滚动视图与标签一起扩展(和收缩),直到它不再展开为止。
  • 我们向Right Label添加约束...导致滚动视图,并且尾随到视图的右边缘,尾部设置为>= 20,压缩阻力设置为1000。这允许标签在扩展/收缩时“粘贴”到滚动视图,但不允许它经过右边缘,也不会压缩超过其自己的内容宽度。
  • 其余约束非常标准。

我们得到:

enter image description here

我掀起了一个简单的例子,它可以让你通过按钮点击来改变文本,看看它在行动:

enter image description here

这是班级:

class ScrollingLabelViewController: UIViewController {

    @IBOutlet var leftLabel: UILabel!
    @IBOutlet var rightLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func leftTap(_ sender: Any) {
        if let btn = sender as? UIButton {

            let t = btn.currentTitle

            switch t {

            case "Short Text":
                leftLabel.text = "Example Name"

            case "Medium Text":
                leftLabel.text = "Example Longer Name"

            case "Long Text":
                fallthrough

            default:
                leftLabel.text = "Example Name that is long enough that it will need to scroll."

            }
        }
    }

    @IBAction func rightTap(_ sender: Any) {
        if let btn = sender as? UIButton {

            let t = btn.currentTitle

            switch t {

            case "Short Text":
                rightLabel.text = "(6)"

            case "Medium Text":
                rightLabel.text = "(255)"

            case "Long Text":
                fallthrough

            default:
                rightLabel.text = "(38,872)"

            }
        }
    }

}

故事板来源:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="itP-Si-dAD">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--Scrolling Label View Controller-->
        <scene sceneID="2HK-o9-I65">
            <objects>
                <viewController id="itP-Si-dAD" customClass="ScrollingLabelViewController" customModule="XC10SWScratch" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="C9r-ad-Wv2">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Rs3-TC-hFe">
                                <rect key="frame" x="20" y="60" width="114.5" height="20.5"/>
                                <subviews>
                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="1000" text="Example Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="3jj-Oq-9XF">
                                        <rect key="frame" x="0.0" y="0.0" width="114.5" height="20.5"/>
                                        <color key="backgroundColor" red="0.0" green="0.99143940210000003" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                        <nil key="textColor"/>
                                        <nil key="highlightedColor"/>
                                    </label>
                                </subviews>
                                <constraints>
                                    <constraint firstAttribute="bottom" secondItem="3jj-Oq-9XF" secondAttribute="bottom" id="7EF-mm-DaK"/>
                                    <constraint firstAttribute="width" secondItem="3jj-Oq-9XF" secondAttribute="width" priority="750" id="ZtX-rP-Lme"/>
                                    <constraint firstAttribute="width" relation="greaterThanOrEqual" constant="12" id="ZyU-il-ReF"/>
                                    <constraint firstItem="3jj-Oq-9XF" firstAttribute="top" secondItem="Rs3-TC-hFe" secondAttribute="top" id="ml2-6X-bBl"/>
                                    <constraint firstItem="3jj-Oq-9XF" firstAttribute="leading" secondItem="Rs3-TC-hFe" secondAttribute="leading" id="r4q-LE-OQ5"/>
                                    <constraint firstAttribute="trailing" secondItem="3jj-Oq-9XF" secondAttribute="trailing" id="t9q-89-BOF"/>
                                    <constraint firstAttribute="height" secondItem="3jj-Oq-9XF" secondAttribute="height" id="xDp-np-bFT"/>
                                </constraints>
                            </scrollView>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="1000" text="(6)" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ZCj-c5-IzQ">
                                <rect key="frame" x="142.5" y="60" width="23" height="20.5"/>
                                <color key="backgroundColor" red="0.0" green="0.99143940210000003" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <stackView opaque="NO" contentMode="scaleToFill" spacing="47" translatesAutoresizingMaskIntoConstraints="NO" id="0v3-eY-MTM">
                                <rect key="frame" x="44" y="160.5" width="287" height="164.5"/>
                                <subviews>
                                    <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="18" translatesAutoresizingMaskIntoConstraints="NO" id="nou-P9-haw">
                                        <rect key="frame" x="0.0" y="0.0" width="120" height="164.5"/>
                                        <subviews>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Left Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Jgc-gS-1Oz">
                                                <rect key="frame" x="0.0" y="0.0" width="120" height="20.5"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="mPG-zF-zdi">
                                                <rect key="frame" x="0.0" y="38.5" width="120" height="30"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <state key="normal" title="Short Text"/>
                                                <connections>
                                                    <action selector="leftTap:" destination="itP-Si-dAD" eventType="touchUpInside" id="z5m-yd-ACM"/>
                                                </connections>
                                            </button>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="xiv-Uc-UPL">
                                                <rect key="frame" x="0.0" y="86.5" width="120" height="30"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <state key="normal" title="Medium Text"/>
                                                <connections>
                                                    <action selector="leftTap:" destination="itP-Si-dAD" eventType="touchUpInside" id="hCp-75-WVo"/>
                                                </connections>
                                            </button>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="LQD-gc-Pv1">
                                                <rect key="frame" x="0.0" y="134.5" width="120" height="30"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <state key="normal" title="Long Text"/>
                                                <connections>
                                                    <action selector="leftTap:" destination="itP-Si-dAD" eventType="touchUpInside" id="fWI-cl-wGZ"/>
                                                </connections>
                                            </button>
                                        </subviews>
                                        <constraints>
                                            <constraint firstAttribute="width" constant="120" id="Mvo-co-QSU"/>
                                        </constraints>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="18" translatesAutoresizingMaskIntoConstraints="NO" id="usT-YQ-P4a">
                                        <rect key="frame" x="167" y="0.0" width="120" height="164.5"/>
                                        <subviews>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Right Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WXA-Lo-BaW">
                                                <rect key="frame" x="0.0" y="0.0" width="120" height="20.5"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="UXZ-r3-dw1">
                                                <rect key="frame" x="0.0" y="38.5" width="120" height="30"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <state key="normal" title="Short Text"/>
                                                <connections>
                                                    <action selector="rightTap:" destination="itP-Si-dAD" eventType="touchUpInside" id="1e8-Dh-iHD"/>
                                                </connections>
                                            </button>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="BN2-Tt-2QG">
                                                <rect key="frame" x="0.0" y="86.5" width="120" height="30"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <state key="normal" title="Medium Text"/>
                                                <connections>
                                                    <action selector="rightTap:" destination="itP-Si-dAD" eventType="touchUpInside" id="8CM-Gx-TYM"/>
                                                </connections>
                                            </button>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vCy-ut-LXL">
                                                <rect key="frame" x="0.0" y="134.5" width="120" height="30"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <state key="normal" title="Long Text"/>
                                                <connections>
                                                    <action selector="rightTap:" destination="itP-Si-dAD" eventType="touchUpInside" id="iTb-4n-jbd"/>
                                                </connections>
                                            </button>
                                        </subviews>
                                        <constraints>
                                            <constraint firstAttribute="width" constant="120" id="ACw-Tb-RfF"/>
                                        </constraints>
                                    </stackView>
                                </subviews>
                            </stackView>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                        <constraints>
                            <constraint firstItem="Rs3-TC-hFe" firstAttribute="leading" secondItem="N2s-Cz-Yp4" secondAttribute="leading" constant="20" id="Dkw-df-pKY"/>
                            <constraint firstItem="ZCj-c5-IzQ" firstAttribute="centerY" secondItem="Rs3-TC-hFe" secondAttribute="centerY" id="GsG-U4-jc6"/>
                            <constraint firstItem="ZCj-c5-IzQ" firstAttribute="leading" secondItem="Rs3-TC-hFe" secondAttribute="trailing" constant="8" id="aVZ-i7-91I"/>
                            <constraint firstItem="Rs3-TC-hFe" firstAttribute="top" secondItem="N2s-Cz-Yp4" secondAttribute="top" constant="40" id="jFX-1U-cRh"/>
                            <constraint firstItem="0v3-eY-MTM" firstAttribute="centerX" secondItem="C9r-ad-Wv2" secondAttribute="centerX" id="jZO-qZ-0z5"/>
                            <constraint firstItem="N2s-Cz-Yp4" firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="ZCj-c5-IzQ" secondAttribute="trailing" constant="20" id="pP1-gC-Uvq"/>
                            <constraint firstItem="0v3-eY-MTM" firstAttribute="top" secondItem="Rs3-TC-hFe" secondAttribute="bottom" constant="80" id="sJi-4A-BOA"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="N2s-Cz-Yp4"/>
                    </view>
                    <connections>
                        <outlet property="leftLabel" destination="3jj-Oq-9XF" id="469-ke-ni2"/>
                        <outlet property="rightLabel" destination="ZCj-c5-IzQ" id="hMG-sN-B29"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="w9F-EF-F89" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="167" y="1506"/>
        </scene>
    </scenes>
</document>

编辑:几乎相同的东西,但设置为XIB用作表视图单元格。

enter image description here

IB中的ScrollingLabelCell.xib

enter image description here

ScrollingLabelCell.xib来源:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
        <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"/>
        <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" id="SVi-M2-eJO" customClass="ScrollingLabelCell" customModule="XC10SWScratch" customModuleProvider="target">
            <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
            <autoresizingMask key="autoresizingMask"/>
            <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="SVi-M2-eJO" id="FO6-8m-8lR">
                <rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
                <autoresizingMask key="autoresizingMask"/>
                <subviews>
                    <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="1TD-My-nqW" userLabel="Inner View">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
                        <subviews>
                            <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="fPZ-Vc-pTX">
                                <rect key="frame" x="16" y="11.5" width="114.5" height="20.5"/>
                                <subviews>
                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Example Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Gsx-qe-uXG">
                                        <rect key="frame" x="0.0" y="0.0" width="114.5" height="20.5"/>
                                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                        <nil key="textColor"/>
                                        <nil key="highlightedColor"/>
                                    </label>
                                </subviews>
                                <constraints>
                                    <constraint firstAttribute="bottom" secondItem="Gsx-qe-uXG" secondAttribute="bottom" id="KNJ-P5-R1c"/>
                                    <constraint firstItem="Gsx-qe-uXG" firstAttribute="top" secondItem="fPZ-Vc-pTX" secondAttribute="top" id="M63-Na-FHd"/>
                                    <constraint firstAttribute="height" secondItem="Gsx-qe-uXG" secondAttribute="height" id="kQb-o9-Cmr"/>
                                    <constraint firstItem="Gsx-qe-uXG" firstAttribute="leading" secondItem="fPZ-Vc-pTX" secondAttribute="leading" id="od5-IF-Drl"/>
                                    <constraint firstAttribute="trailing" secondItem="Gsx-qe-uXG" secondAttribute="trailing" id="pVh-95-8bc"/>
                                    <constraint firstAttribute="width" secondItem="Gsx-qe-uXG" secondAttribute="width" priority="750" id="zUu-pN-rme"/>
                                </constraints>
                            </scrollView>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="1000" text="(6)" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="cnK-Pa-DDF">
                                <rect key="frame" x="142.5" y="11.5" width="23" height="21"/>
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                        </subviews>
                        <constraints>
                            <constraint firstItem="fPZ-Vc-pTX" firstAttribute="leading" secondItem="1TD-My-nqW" secondAttribute="leading" constant="16" id="24S-Dy-Pf7"/>
                            <constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="cnK-Pa-DDF" secondAttribute="trailing" constant="20" id="9P7-Or-S8M"/>
                            <constraint firstAttribute="bottom" secondItem="fPZ-Vc-pTX" secondAttribute="bottom" constant="11.5" id="BD3-7Z-Ry9"/>
                            <constraint firstItem="cnK-Pa-DDF" firstAttribute="centerY" secondItem="fPZ-Vc-pTX" secondAttribute="centerY" id="Q4G-6M-3Qe"/>
                            <constraint firstItem="fPZ-Vc-pTX" firstAttribute="top" secondItem="1TD-My-nqW" secondAttribute="top" constant="11.5" id="lOh-MF-Khj"/>
                            <constraint firstItem="cnK-Pa-DDF" firstAttribute="leading" secondItem="fPZ-Vc-pTX" secondAttribute="trailing" constant="12" id="mVz-me-wRN"/>
                        </constraints>
                    </view>
                </subviews>
                <constraints>
                    <constraint firstAttribute="bottom" secondItem="1TD-My-nqW" secondAttribute="bottom" id="fgL-GE-OC3"/>
                    <constraint firstItem="1TD-My-nqW" firstAttribute="leading" secondItem="FO6-8m-8lR" secondAttribute="leading" id="iV5-qZ-Oq7"/>
                    <constraint firstAttribute="trailing" secondItem="1TD-My-nqW" secondAttribute="trailing" id="whk-Ke-oc7"/>
                    <constraint firstItem="1TD-My-nqW" firstAttribute="top" secondItem="FO6-8m-8lR" secondAttribute="top" id="yIU-Lz-jlC"/>
                </constraints>
            </tableViewCellContentView>
            <connections>
                <outlet property="leftLabel" destination="Gsx-qe-uXG" id="9kR-67-2oC"/>
                <outlet property="rightLabel" destination="cnK-Pa-DDF" id="p4k-MJ-gb2"/>
            </connections>
            <point key="canvasLocation" x="156" y="50.374812593703155"/>
        </tableViewCell>
    </objects>
</document>

和示例单元格和表视图控制器类:

class ScrollingLabelCell: UITableViewCell {

    @IBOutlet var leftLabel: UILabel!
    @IBOutlet var rightLabel: UILabel!

}

class ScrollingLabelTableViewController: UITableViewController {

    let xibName = "ScrollingLabelCell"

    let theData: [[String]] = [
        ["Example Name", "1"],
        ["Example Longer Name", "2"],
        ["Example Even Longer Name", "3"],
        ["To demonstrate the scrolling,", "4"],
        ["6, 7 & 800 have the same name", "5"],
        ["Mr. Example Name that is Long Enough that it Needs to Scroll", "6"],
        ["Mr. Example Name that is Long Enough that it Needs to Scroll", "7"],
        ["Mr. Example Name that is Long Enough that it Needs to Scroll", "800"],
        ["Back to Short Names", "99"],
        ["With longer numbers", "2,750"],
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        let bundle = Bundle(for: type(of: self))
        let cellNib = UINib(nibName: xibName, bundle: bundle)
        tableView.register(cellNib, forCellReuseIdentifier: xibName)

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return theData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: xibName, for: indexPath) as! ScrollingLabelCell

        cell.leftLabel.text = theData[indexPath.row][0]
        cell.rightLabel.text = "(" + theData[indexPath.row][1] + ")"

        return cell
    }

}

0
投票

您可以使用约束来完成此操作,或者您可以尝试使用stackViews,因为这通常是它们的预期用途:

UIStackview

| Label 1 | UIScrollView (Label 2) |

在这种情况下,只需将scrollView的宽度约束添加到> =比预期大小,并增加label 1的垂直优先级。

我还会重新考虑将标签放在scrollView中,在另一个scrollView(在这个特定情况下是你的tableView)里面,因为要获得一些小的滚动它会非常不舒服。可能更好的是只考虑将标签放在另一个之下或其他一些布局改进,需要更少的调整和更好的用户体验。

© www.soinside.com 2019 - 2024. All rights reserved.