Ios 根据UIScrollView中的内容调整UIView的大小
我在根UIView中有这样的布局层次结构,请参见图片: UIScrollView->UIView->UITextView 我在UITextView.textViewDidChange上的代码中动态更改UIScrollView视图的大小。 我对UIView.bottom有一个与Superview.bottom UIScrollView相等的约束。 在UITextView视图适合一个屏幕之前,一切都正常工作 如何以编程方式将UIView调整为等于UITextView大小Ios 根据UIScrollView中的内容调整UIView的大小,ios,swift,uiview,uiscrollview,uitextview,Ios,Swift,Uiview,Uiscrollview,Uitextview,我在根UIView中有这样的布局层次结构,请参见图片: UIScrollView->UIView->UITextView 我在UITextView.textViewDidChange上的代码中动态更改UIScrollView视图的大小。 我对UIView.bottom有一个与Superview.bottom UIScrollView相等的约束。 在UITextView视图适合一个屏幕之前,一切都正常工作 如何以编程方式将UIView调整为等于UITextView大小 您需要做的是: 将UITex
您需要做的是: 将UITextView顶部/前导/尾随约束到其超级视图UIView 将UITextView底部约束到其superview底部UIView,使用>= 将UIView上/下/前导/尾随约束到其超级视图UIScrollView 将UIView的宽度和高度约束为其超级视图UIScrollView的宽度和高度 编辑高度约束并设置优先级: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>
在运行时:
显示大量文本并向上滚动:
下面是故事板的来源—不需要代码—只有故事板:
<?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>
您可以为此创建一个约束吗,例如UIView大小为X,在textViewDidChange上使用constraint.constant=newSizeIt除了在某些情况下可以工作外,但我得到一个错误:无法同时满足约束。如果UIScrollView中有UITextView,是否可以删除scrollView和ViewView垂直中心的约束?禁用文本视图上的滚动。然后,它将根据文本自动调整其高度。像使用任何其他视图一样使用约束,例如,行数设置为零的UILabel。@m1sh0,我在UIBuilder中遇到错误-需要约束:Y位置或高度