根据其内容调整 UIScrollView 中 UIView 的大小

Adjust size of UIView inside UIScrollView according to its content

我在根 UIView 内部有这样的布局层次结构(见图): UIScrollView -> UIView -> UITextView

我在 UITextView.textViewDidChange 的代码中动态更改 UIScrollView 视图的大小。 我对 UIView.bottom 有一个等于 Superview.bottom (UIScrollView) 的约束。 一切正常,直到 UITextView 视图适合一个屏幕。

如何以编程方式调整 UIView 以等于 UITextView 大小。

您需要做的是:

  • UITextView 顶部/前导/尾部约束到其父视图(UIView
  • UITextView 底部 约束到它的父视图底部(UIView),>=
  • UIView 顶部/底部/前导/尾部约束到其父视图(UIScrollView
  • 约束 UIView 宽度和高度等于其父视图(UIScrollView)的宽度和高度
  • 编辑高度限制并设置Priority: 250

这将允许文本视图根据其内容垂直增长/收缩。 UIView 将保持在滚动视图的高度,直到文本视图变得太高而无法容纳。那时,随着文本视图的增长,低优先级将允许 UIView 的底边成为 "pushed down"。

故事板中的布局如下:

运行时:

有很多文字并向上滚动:

这是情节提要的来源(无需代码 - 只需情节提要):

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="VM1-mg-1mG">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="jpE-FH-8e8">
            <objects>
                <viewController id="VM1-mg-1mG" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="yS7-gh-XNz">
                        <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="gBx-Cd-lhB">
                                <rect key="frame" x="8" y="120" width="359" height="447"/>
                                <subviews>
                                    <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="UGW-Gc-H5B">
                                        <rect key="frame" x="0.0" y="0.0" width="359" height="447"/>
                                        <subviews>
                                            <textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" text="This is the UITextView" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="wad-lw-VgC">
                                                <rect key="frame" x="20" y="20" width="319" height="45"/>
                                                <color key="backgroundColor" red="0.99942404029999998" green="0.98555368190000003" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="24"/>
                                                <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
                                            </textView>
                                        </subviews>
                                        <color key="backgroundColor" red="0.52747867609999999" green="1" blue="0.55622484120000004" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <constraints>
                                            <constraint firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="wad-lw-VgC" secondAttribute="bottom" constant="20" id="43l-VA-TfK"/>
                                            <constraint firstItem="wad-lw-VgC" firstAttribute="top" secondItem="UGW-Gc-H5B" secondAttribute="top" constant="20" id="6gx-L4-rfK"/>
                                            <constraint firstAttribute="trailing" secondItem="wad-lw-VgC" secondAttribute="trailing" constant="20" id="Hze-tT-ctt"/>
                                            <constraint firstItem="wad-lw-VgC" firstAttribute="leading" secondItem="UGW-Gc-H5B" secondAttribute="leading" constant="20" id="O7m-RX-NHL"/>
                                        </constraints>
                                    </view>
                                </subviews>
                                <color key="backgroundColor" red="0.46202266219999999" green="0.83828371759999998" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                <constraints>
                                    <constraint firstItem="UGW-Gc-H5B" firstAttribute="top" secondItem="gBx-Cd-lhB" secondAttribute="top" id="9Ca-8i-QSo"/>
                                    <constraint firstAttribute="trailing" secondItem="UGW-Gc-H5B" secondAttribute="trailing" id="HpF-K7-pGb"/>
                                    <constraint firstItem="UGW-Gc-H5B" firstAttribute="width" secondItem="gBx-Cd-lhB" secondAttribute="width" id="T85-Nb-vFG"/>
                                    <constraint firstItem="UGW-Gc-H5B" firstAttribute="leading" secondItem="gBx-Cd-lhB" secondAttribute="leading" id="aKi-pE-7nq"/>
                                    <constraint firstItem="UGW-Gc-H5B" firstAttribute="height" secondItem="gBx-Cd-lhB" secondAttribute="height" priority="250" id="gMZ-MY-cZ4"/>
                                    <constraint firstAttribute="bottom" secondItem="UGW-Gc-H5B" secondAttribute="bottom" id="yBi-9n-Sd3"/>
                                </constraints>
                            </scrollView>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                        <constraints>
                            <constraint firstItem="gBx-Cd-lhB" firstAttribute="top" secondItem="1Le-6E-02N" secondAttribute="top" constant="100" id="IpO-b2-zbv"/>
                            <constraint firstItem="1Le-6E-02N" firstAttribute="trailing" secondItem="gBx-Cd-lhB" secondAttribute="trailing" constant="8" id="XVi-7E-3hn"/>
                            <constraint firstItem="gBx-Cd-lhB" firstAttribute="leading" secondItem="1Le-6E-02N" secondAttribute="leading" constant="8" id="at7-bP-fws"/>
                            <constraint firstItem="1Le-6E-02N" firstAttribute="bottom" secondItem="gBx-Cd-lhB" secondAttribute="bottom" constant="100" id="nhL-ST-z4d"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="1Le-6E-02N"/>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="6C7-lr-bGI" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="136.80000000000001" y="124.58770614692655"/>
        </scene>
    </scenes>
</document>