首页 > 解决方案 > 根据内容调整 UIScrollView 内部 UIView 的大小

问题描述

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

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

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

在此处输入图像描述

标签: iosswiftuiviewuiscrollviewuitextview

解决方案


你需要做的是:

  • UITextView将顶部/前导/尾随约束到其超级视图(the UIView
  • UITextView 将底部约束到其超级视图底部 (the UIView),使用>=
  • UIView将顶部/底部/前导/尾随约束到其超级视图(the UIScrollView
  • 约束UIView宽度和高度等于其父视图的宽度和高度(UIScrollView
  • 编辑高度约束并设置Priority: 250

这将允许文本视图根据其内容垂直增长/缩小。将UIView保持在滚动视图的高度,直到文本视图变得太高而无法容纳。此时,低优先级将允许UIView随着文本视图的增长而“下推”底部边缘。

这是故事板中的布局:

在此处输入图像描述

在运行时:

在此处输入图像描述

有很多文字并向上滚动:

在此处输入图像描述

这是故事板的来源(不需要代码 - 只是故事板):

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

推荐阅读