我在根temp
中有这样的布局层次结构(参见图片):
self.head= temp
-> UIView
-> UIScrollView
我在UIView
。UITextView
上的代码中动态更改UIScrollView
视图的大小。
我对UITextView
的约束等于textViewDidChange
(UIView.bottom
)。
一切正常,直到Superview.bottom
视图适合一个屏幕为止。
如何通过编程方式将UIScrollView
调整为等于UITextView
的大小。
答案 0 :(得分:1)
您需要做的是:
UITextView
的顶部/前导/尾随其超级视图(UIView
)UITextView
UIView
底部约束到其超级视图底部(>=
)。
UIView
的顶部/底部/前导/尾随其超级视图(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>