swift - 带有容器 View 的 ScrollView 不能正确调整内容大小

标签 swift uiscrollview uikit

我有一个这样排列的 View 层次结构:

main view
---- scroll view
-------- label
-------- label
-------- text view
-------- horizontal stack view
-------- divider view (UIView)
-------- label
-------- container view (embeds a child view controller)

嵌入式 subview Controller 是一个 UICollectionViewController

现在的问题是 Collection View Controller 包含很多元素,我希望 ScrollView 能够自动调整其内容大小,以便也考虑到 Collection View 的高度,但事实并非如此。所以目前我只能看到 Collection View 的第一个元素,但无法向下滚动。我尝试通过这种方式手动调整 Collection View 的内容大小:

func adjustContentSize() {
    var contentRect: CGRect = .zero

    for subview in self.scrollView.subviews {
        contentRect = contentRect.union(subview.frame)
    }

    self.scrollView.contentSize = contentRect.size
}

但是内容大小仍然不正确:它正好等于窗口大小,即使它应该高很多以便为 Collection View 中的所有元素腾出位置。我认为问题主要与我嵌入 subview Controller 这一事实有关,因为通常 ScrollView 内容大小会自动调整。但是在这种情况下该怎么办?如何告诉 ScrollView 正确的内容大小?

最佳答案

在容器 View 上设置约束与在任何其他 View 上没有什么不同。

这是你的布局,有约束:

enter image description here

在运行时,它看起来像这样:

enter image description here

并且,滚动:

enter image description here

这里是所有需要的代码( Collection View 使用水平流):

//
//  ViewController.swift
//  Ramy
//
//  Created by Don Mag on 8/7/19.
//

import UIKit

class ViewController: UIViewController {

}

extension UIColor {
    static var random: UIColor {
        return UIColor(red: .random(in: 0...1),
                       green: .random(in: 0...1),
                       blue: .random(in: 0...1),
                       alpha: 1.0)
    }
}

class EmbeddedCollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "ReuseID")
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReuseID", for: indexPath)
        cell.backgroundColor = UIColor.random
        return cell

    }

}

Storyboard的来源:

<?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="BYZ-38-t0r">
    <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="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Ramy" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <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="jJS-Jp-5Wb">
                                <rect key="frame" x="8" y="28" width="359" height="631"/>
                                <subviews>
                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label 1" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="XwC-bl-fns">
                                        <rect key="frame" x="153" y="8" width="53.5" height="20.5"/>
                                        <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                        <nil key="textColor"/>
                                        <nil key="highlightedColor"/>
                                    </label>
                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label 2" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VcB-Di-yle">
                                        <rect key="frame" x="151.5" y="40.5" width="56" height="20.5"/>
                                        <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" 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" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="2P9-JD-ezE">
                                        <rect key="frame" x="59.5" y="73" width="240" height="128"/>
                                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                        <constraints>
                                            <constraint firstAttribute="width" constant="240" id="RSb-O4-Jy1"/>
                                            <constraint firstAttribute="height" constant="128" id="ZNr-Tq-dig"/>
                                        </constraints>
                                        <string key="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.</string>
                                        <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                        <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
                                    </textView>
                                    <stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="Urd-av-tKj">
                                        <rect key="frame" x="54" y="213" width="251.5" height="20.5"/>
                                        <subviews>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Horizontal" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iJ9-BV-XXm">
                                                <rect key="frame" x="0.0" y="0.0" width="78.5" height="20.5"/>
                                                <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Stack" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="SlW-MH-lZj">
                                                <rect key="frame" x="86.5" y="0.0" width="78.5" height="20.5"/>
                                                <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="View" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="usj-S2-wXe">
                                                <rect key="frame" x="173" y="0.0" width="78.5" height="20.5"/>
                                                <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                        </subviews>
                                    </stackView>
                                    <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Ube-Ox-WG0" userLabel="Divider View">
                                        <rect key="frame" x="29.5" y="253.5" width="300" height="4"/>
                                        <color key="backgroundColor" red="1" green="0.14913141730000001" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <constraints>
                                            <constraint firstAttribute="width" constant="300" id="3r0-U8-e4h"/>
                                            <constraint firstAttribute="height" constant="4" id="8Zh-29-VuE"/>
                                        </constraints>
                                    </view>
                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label 3" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="t4g-6L-Fxz">
                                        <rect key="frame" x="151.5" y="277.5" width="56.5" height="20.5"/>
                                        <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                        <nil key="textColor"/>
                                        <nil key="highlightedColor"/>
                                    </label>
                                    <containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Ae9-nr-NPW">
                                        <rect key="frame" x="12" y="310" width="335" height="500"/>
                                        <color key="backgroundColor" red="0.16078431369999999" green="0.62352941179999999" blue="0.81960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <constraints>
                                            <constraint firstAttribute="height" constant="500" id="Ugi-NN-btd"/>
                                        </constraints>
                                        <connections>
                                            <segue destination="iKt-In-mH7" kind="embed" id="xc0-HZ-ykL"/>
                                        </connections>
                                    </containerView>
                                </subviews>
                                <color key="backgroundColor" red="0.99942404029999998" green="0.88699374509999995" blue="0.6455106089" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                <constraints>
                                    <constraint firstItem="Ae9-nr-NPW" firstAttribute="leading" secondItem="jJS-Jp-5Wb" secondAttribute="leading" constant="12" id="46G-Do-tx2"/>
                                    <constraint firstItem="Ae9-nr-NPW" firstAttribute="width" secondItem="jJS-Jp-5Wb" secondAttribute="width" constant="-24" id="9Gx-Qt-KD7"/>
                                    <constraint firstItem="VcB-Di-yle" firstAttribute="centerX" secondItem="2P9-JD-ezE" secondAttribute="centerX" id="AbA-b2-Dc2"/>
                                    <constraint firstItem="Ube-Ox-WG0" firstAttribute="centerX" secondItem="t4g-6L-Fxz" secondAttribute="centerX" id="J1X-MF-qh8"/>
                                    <constraint firstAttribute="bottom" secondItem="Ae9-nr-NPW" secondAttribute="bottom" constant="8" id="J4e-Iv-CbW"/>
                                    <constraint firstItem="VcB-Di-yle" firstAttribute="top" secondItem="XwC-bl-fns" secondAttribute="bottom" constant="12" id="Lbs-Pa-kn0"/>
                                    <constraint firstItem="Urd-av-tKj" firstAttribute="top" secondItem="2P9-JD-ezE" secondAttribute="bottom" constant="12" id="MnP-kq-VMd"/>
                                    <constraint firstItem="Ube-Ox-WG0" firstAttribute="top" secondItem="Urd-av-tKj" secondAttribute="bottom" constant="20" id="Nzj-mz-NBe"/>
                                    <constraint firstItem="2P9-JD-ezE" firstAttribute="top" secondItem="VcB-Di-yle" secondAttribute="bottom" constant="12" id="PV8-rx-82P"/>
                                    <constraint firstItem="t4g-6L-Fxz" firstAttribute="centerX" secondItem="Ae9-nr-NPW" secondAttribute="centerX" id="R8B-Yu-eaX"/>
                                    <constraint firstAttribute="trailing" secondItem="Ae9-nr-NPW" secondAttribute="trailing" constant="12" id="Vl6-pC-fR7"/>
                                    <constraint firstItem="t4g-6L-Fxz" firstAttribute="top" secondItem="Ube-Ox-WG0" secondAttribute="bottom" constant="20" id="dzM-uC-vDg"/>
                                    <constraint firstItem="2P9-JD-ezE" firstAttribute="centerX" secondItem="Urd-av-tKj" secondAttribute="centerX" id="gIG-Ln-0ci"/>
                                    <constraint firstItem="XwC-bl-fns" firstAttribute="top" secondItem="jJS-Jp-5Wb" secondAttribute="top" constant="8" id="h2g-bu-iYg"/>
                                    <constraint firstItem="Ae9-nr-NPW" firstAttribute="top" secondItem="t4g-6L-Fxz" secondAttribute="bottom" constant="12" id="k5A-Ht-wuD"/>
                                    <constraint firstItem="XwC-bl-fns" firstAttribute="centerX" secondItem="VcB-Di-yle" secondAttribute="centerX" id="s7H-0G-55o"/>
                                    <constraint firstItem="Urd-av-tKj" firstAttribute="centerX" secondItem="Ube-Ox-WG0" secondAttribute="centerX" id="wIn-jA-Hwn"/>
                                </constraints>
                            </scrollView>
                        </subviews>
                        <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="jJS-Jp-5Wb" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="8" id="HZI-i3-eQL"/>
                            <constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="jJS-Jp-5Wb" secondAttribute="trailing" constant="8" id="TrF-oh-lbk"/>
                            <constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="jJS-Jp-5Wb" secondAttribute="bottom" constant="8" id="gCW-Kv-Vma"/>
                            <constraint firstItem="jJS-Jp-5Wb" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="8" id="mdg-vi-3RB"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="138.40000000000001" y="138.98050974512745"/>
        </scene>
        <!--Embedded Collection View Controller-->
        <scene sceneID="8Wg-59-nK4">
            <objects>
                <collectionViewController id="iKt-In-mH7" customClass="EmbeddedCollectionViewController" customModule="Ramy" customModuleProvider="target" sceneMemberID="viewController">
                    <collectionView key="view" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" id="pbM-fa-YQc">
                        <rect key="frame" x="0.0" y="0.0" width="335" height="500"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                        <collectionViewFlowLayout key="collectionViewLayout" scrollDirection="horizontal" minimumLineSpacing="10" minimumInteritemSpacing="10" id="Sd4-5y-JzD">
                            <size key="itemSize" width="50" height="50"/>
                            <size key="headerReferenceSize" width="0.0" height="0.0"/>
                            <size key="footerReferenceSize" width="0.0" height="0.0"/>
                            <inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
                        </collectionViewFlowLayout>
                        <cells>
                            <collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" misplaced="YES" id="UTC-Yv-lrd">
                                <rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
                                <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                <view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO">
                                    <rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
                                    <autoresizingMask key="autoresizingMask"/>
                                </view>
                            </collectionViewCell>
                        </cells>
                        <connections>
                            <outlet property="dataSource" destination="iKt-In-mH7" id="KsO-Bh-EEm"/>
                            <outlet property="delegate" destination="iKt-In-mH7" id="IOW-gi-oV8"/>
                        </connections>
                    </collectionView>
                </collectionViewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="PLD-Y7-bEq" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="888.79999999999995" y="312.14392803598201"/>
        </scene>
    </scenes>
</document>

关于swift - 带有容器 View 的 ScrollView 不能正确调整内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57348478/

相关文章:

ios - 在 View 中使用 AVPlayer 播放多个视频

iOS Swift 3 在显示键盘时将 TableView 内容保持在正确的位置

ios - 如何在 Swift 中创建一个 UILabel 数组

ios - 当 superview 布置 subview 时,UISearchBar 动画到位。如何禁用?

ios - 在 UIViewController 之间传递 UILongPressGestureRecognizer

ios - 如何返回此函数的结果?

ios - Swift iOS - 获取字典以接受零值

ios - 应用程序崩溃但在调试器控制台中仍然显示 "running..."

ios - UIScrollView动画的height和contentOffset "jumps"content from bottom

iphone - 如何获取随机 UIColor 但将其限制为颜色的子集