在UIScrollView中自动调整UITextView的大小

时间:2019-01-31 17:19:10

标签: ios swift uiscrollview uitextview uistoryboard

我正在设置一个显示某些内容的滚动视图。我试图弄清楚如何根据文本视图内的文本量来更改滚动视图的大小(当然也要更改文本视图的高度,但保持宽度不变)。我将情节提要用于滚动视图。在视图控制器内部是滚动视图,其中包含一个容器视图(这是保存文本视图的位置)。我正在谈论的文本视图位于内容描述的下方。我尝试了许多不同的方法,但没有找到一种可行的方法。任何帮助将不胜感激。谢谢! (还值得注意的是,我正在使用DeckTransition库呈现此视图控制器)

我的Xcode故事板https://imgur.com/OFrKRSk 到目前为止在模拟器https://imgur.com/iq73Jn0

中的外观

3 个答案:

答案 0 :(得分:1)

这是一个可以尝试的基本示例。点击“更改文本”按钮将在文本视图中的短,中和长文本之间切换,从而得到以下结果:

Scribbletune enter image description here enter image description here

这里是所有需要的代码(它所做的就是更改文本-别无其他):

import UIKit

class TextViewinScrollViewController: UIViewController {

    @IBOutlet var theTextView: UITextView!

    var i = 0

    var strings = [
        "This is a Short amount of text.",
        "This is a Medium amount of text. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
        "This is a Long amount of text. Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        theTextView.text = strings[i]
    }

    @IBAction func didTap(_ sender: Any) {
        i += 1
        if i >= strings.count {
            i = 0
        }
        theTextView.text = strings[i]
    }

}

这是情节提要:

<?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="HAr-gQ-Ow4">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <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>
        <!--Text Viewin Scroll View Controller-->
        <scene sceneID="0zs-mA-Zcw">
            <objects>
                <viewController id="HAr-gQ-Ow4" customClass="TextViewinScrollViewController" customModule="XC10SWScratch" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="oNR-og-qiK">
                        <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="XBn-Je-V51">
                                <rect key="frame" x="40" y="60" width="295" height="567"/>
                                <subviews>
                                    <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="oNQ-i1-b49" userLabel="Container">
                                        <rect key="frame" x="0.0" y="0.0" width="295" height="675.5"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="VAf-T0-cKT">
                                                <rect key="frame" x="0.0" y="0.0" width="295" height="240"/>
                                                <color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                <constraints>
                                                    <constraint firstAttribute="height" constant="240" id="NzL-9L-Ybs"/>
                                                </constraints>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="K3u-yi-BbX">
                                                <rect key="frame" x="51.5" y="252" width="192" height="26"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <inset key="contentEdgeInsets" minX="10" minY="4" maxX="10" maxY="4"/>
                                                <state key="normal" title="Change Text in Text View"/>
                                                <connections>
                                                    <action selector="didTap:" destination="HAr-gQ-Ow4" eventType="touchUpInside" id="dnw-ff-qw3"/>
                                                </connections>
                                            </button>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Content Description" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="gXD-0q-Avm">
                                                <rect key="frame" x="8" y="290" width="153.5" height="20.5"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                            <textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" text="This is the Text View" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="Mwg-70-9L2">
                                                <rect key="frame" x="20" y="322.5" width="255" height="33"/>
                                                <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                                <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
                                            </textView>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1ox-xZ-Teo" userLabel="Example View">
                                                <rect key="frame" x="20" y="367.5" width="255" height="300"/>
                                                <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <constraints>
                                                    <constraint firstAttribute="height" constant="300" id="hXk-ck-CvF"/>
                                                </constraints>
                                                <string key="text">Example View
(300-pts Height)</string>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                        </subviews>
                                        <color key="backgroundColor" red="1" green="0.83234566450000003" blue="0.47320586440000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <constraints>
                                            <constraint firstItem="gXD-0q-Avm" firstAttribute="top" secondItem="K3u-yi-BbX" secondAttribute="bottom" constant="12" id="7gX-pC-zmh"/>
                                            <constraint firstItem="Mwg-70-9L2" firstAttribute="width" secondItem="oNQ-i1-b49" secondAttribute="width" constant="-40" id="9pZ-ja-g7R"/>
                                            <constraint firstItem="Mwg-70-9L2" firstAttribute="centerX" secondItem="oNQ-i1-b49" secondAttribute="centerX" id="Byo-5N-WXf"/>
                                            <constraint firstAttribute="trailing" secondItem="VAf-T0-cKT" secondAttribute="trailing" id="FyF-by-edM"/>
                                            <constraint firstItem="1ox-xZ-Teo" firstAttribute="width" secondItem="Mwg-70-9L2" secondAttribute="width" id="Lic-3Q-vWZ"/>
                                            <constraint firstAttribute="bottom" secondItem="1ox-xZ-Teo" secondAttribute="bottom" constant="8" id="MQK-MS-dCA"/>
                                            <constraint firstItem="K3u-yi-BbX" firstAttribute="centerX" secondItem="oNQ-i1-b49" secondAttribute="centerX" id="UPN-FD-gxb"/>
                                            <constraint firstItem="K3u-yi-BbX" firstAttribute="top" secondItem="VAf-T0-cKT" secondAttribute="bottom" constant="12" id="WMi-Vl-xBu"/>
                                            <constraint firstItem="1ox-xZ-Teo" firstAttribute="centerX" secondItem="oNQ-i1-b49" secondAttribute="centerX" id="dno-nR-zvQ"/>
                                            <constraint firstItem="Mwg-70-9L2" firstAttribute="top" secondItem="gXD-0q-Avm" secondAttribute="bottom" constant="12" id="fDe-GP-gs4"/>
                                            <constraint firstItem="VAf-T0-cKT" firstAttribute="top" secondItem="oNQ-i1-b49" secondAttribute="top" id="hRj-XY-fnb"/>
                                            <constraint firstItem="gXD-0q-Avm" firstAttribute="leading" secondItem="oNQ-i1-b49" secondAttribute="leading" constant="8" id="ks5-IH-yrt"/>
                                            <constraint firstItem="1ox-xZ-Teo" firstAttribute="top" secondItem="Mwg-70-9L2" secondAttribute="bottom" constant="12" id="lYY-L9-pb6"/>
                                            <constraint firstItem="VAf-T0-cKT" firstAttribute="leading" secondItem="oNQ-i1-b49" secondAttribute="leading" id="urO-i9-GQ7"/>
                                        </constraints>
                                    </view>
                                </subviews>
                                <color key="backgroundColor" red="0.46202266219999999" green="0.83828371759999998" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                <constraints>
                                    <constraint firstItem="oNQ-i1-b49" firstAttribute="top" secondItem="XBn-Je-V51" secondAttribute="top" id="FGL-wN-OHg"/>
                                    <constraint firstItem="oNQ-i1-b49" firstAttribute="leading" secondItem="XBn-Je-V51" secondAttribute="leading" id="f4T-Mh-ZdB"/>
                                    <constraint firstAttribute="bottom" secondItem="oNQ-i1-b49" secondAttribute="bottom" id="qwX-hf-UtO"/>
                                    <constraint firstAttribute="trailing" secondItem="oNQ-i1-b49" secondAttribute="trailing" id="tdW-x1-DtX"/>
                                    <constraint firstItem="oNQ-i1-b49" firstAttribute="width" secondItem="XBn-Je-V51" secondAttribute="width" id="urs-jZ-3Hi"/>
                                </constraints>
                            </scrollView>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                        <constraints>
                            <constraint firstItem="VsF-QR-ZgP" firstAttribute="trailing" secondItem="XBn-Je-V51" secondAttribute="trailing" constant="40" id="bww-Xy-uZ6"/>
                            <constraint firstItem="XBn-Je-V51" firstAttribute="top" secondItem="VsF-QR-ZgP" secondAttribute="top" constant="40" id="dP4-Ax-cgr"/>
                            <constraint firstItem="XBn-Je-V51" firstAttribute="leading" secondItem="VsF-QR-ZgP" secondAttribute="leading" constant="40" id="eof-fi-9Ml"/>
                            <constraint firstItem="VsF-QR-ZgP" firstAttribute="bottom" secondItem="XBn-Je-V51" secondAttribute="bottom" constant="40" id="euP-M7-rBT"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="VsF-QR-ZgP"/>
                    </view>
                    <connections>
                        <outlet property="theTextView" destination="Mwg-70-9L2" id="IWV-hn-TRM"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="q25-D5-Cge" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="-114.40000000000001" y="58.920539730134934"/>
        </scene>
    </scenes>
</document>

答案 1 :(得分:0)

如果将scrollEnabled设置为false,则文本视图将适合其内容。如果不起作用,则应检查约束。例如内容优先,抗压性优先。为了确保文本视图缩小到小于内部内容大小。

答案 2 :(得分:0)

好的,事实证明,我有一些约束,这些约束不允许文本视图正确调整大小。我禁用了在文本视图上的滚动,删除了在滚动视图和内容视图上的高度限制,现在一切似乎都在工作。我感谢所有的建议!你们超级有帮助!