Ios 使一个UIView组件可以上下滚动

Ios 使一个UIView组件可以上下滚动,ios,uiview,uiscrollview,Ios,Uiview,Uiscrollview,在下面的屏幕截图中,蓝色区域是我的UIScrollView,黄色区域是UIView组件 我想实现的是,iPhone上只有黄色区域可以上下移动。(我将在黄色UI视图上添加更多UI组件,它们应作为一个整体移动/滚动) 我现在陷入困境,我不确定我是否在正确的方向上实现它。有人能告诉我正确的方向吗 (我还需要将蓝色背景更改为不可滚动的图像)尝试将黄色视图放在长透明视图中,将透明视图放在滚动视图中。调整透明视图的高度和scrollView内容插入以调整黄色视图的滚动范围。尝试将黄色视图放置在长透明视图中

在下面的屏幕截图中,蓝色区域是我的UIScrollView,黄色区域是UIView组件

我想实现的是,iPhone上只有黄色区域可以上下移动。(我将在黄色UI视图上添加更多UI组件,它们应作为一个整体移动/滚动)

我现在陷入困境,我不确定我是否在正确的方向上实现它。有人能告诉我正确的方向吗


(我还需要将蓝色背景更改为不可滚动的图像)

尝试将黄色视图放在长透明视图中,将透明视图放在滚动视图中。调整透明视图的高度和scrollView内容插入以调整黄色视图的滚动范围。

尝试将黄色视图放置在长透明视图中,并将透明视图放置在scrollView中。调整透明视图和scrollView内容插入的高度,以调整黄色视图的滚动范围。

我已经创建了演示,对我来说,它在storyboard
XML

  <!--View Controller Test-->
    <scene sceneID="Mea-LA-Bdd">
        <objects>
            <viewController id="apb-o6-bca" customClass="ViewControllerTest" customModule="StoryBoard" customModuleProvider="target" sceneMemberID="viewController">
                <layoutGuides>
                    <viewControllerLayoutGuide type="top" id="M5h-pq-oJT"/>
                    <viewControllerLayoutGuide type="bottom" id="Szm-1V-GvH"/>
                </layoutGuides>
                <view key="view" contentMode="scaleToFill" id="6Fa-fa-e1T">
                    <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <subviews>
                        <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Cy8-Tt-uZI">
                            <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                            <subviews>
                                <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="YI6-Cd-VzM">
                                    <rect key="frame" x="0.0" y="0.0" width="768" height="1792"/>
                                    <subviews>
                                        <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2a3-Zk-7dR">
                                            <rect key="frame" x="0.0" y="768" width="768" height="256"/>
                                            <color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
                                            <constraints>
                                                <constraint firstAttribute="height" constant="256" id="hFq-kg-mhd"/>
                                            </constraints>
                                        </view>
                                    </subviews>
                                    <color key="backgroundColor" red="0.87843137250000003" green="0.89411764709999997" blue="0.89411764709999997" alpha="1" colorSpace="calibratedRGB"/>
                                    <constraints>
                                        <constraint firstItem="2a3-Zk-7dR" firstAttribute="centerY" secondItem="YI6-Cd-VzM" secondAttribute="centerY" id="N66-2N-D4i"/>
                                        <constraint firstItem="2a3-Zk-7dR" firstAttribute="leading" secondItem="YI6-Cd-VzM" secondAttribute="leading" id="avT-2f-PKx"/>
                                        <constraint firstAttribute="trailing" secondItem="2a3-Zk-7dR" secondAttribute="trailing" id="nnJ-PW-ft4"/>
                                    </constraints>
                                </view>
                            </subviews>
                            <color key="backgroundColor" red="0.68627450980000004" green="0.72549019609999998" blue="0.74901960779999999" alpha="1" colorSpace="calibratedRGB"/>
                            <constraints>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="top" secondItem="Cy8-Tt-uZI" secondAttribute="top" id="C4r-zI-jfx"/>
                                <constraint firstAttribute="bottom" secondItem="YI6-Cd-VzM" secondAttribute="bottom" id="NDs-Z7-us0"/>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="height" secondItem="Cy8-Tt-uZI" secondAttribute="height" constant="768" id="NnT-HW-g61"/>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="width" secondItem="Cy8-Tt-uZI" secondAttribute="width" id="W7n-ym-257"/>
                                <constraint firstAttribute="trailing" secondItem="YI6-Cd-VzM" secondAttribute="trailing" id="bWQ-Fn-3rw"/>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="leading" secondItem="Cy8-Tt-uZI" secondAttribute="leading" id="dTM-iV-SPc"/>
                            </constraints>
                            <edgeInsets key="layoutMargins" top="0.0" left="0.0" bottom="0.0" right="0.0"/>
                        </scrollView>
                    </subviews>
                    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                    <constraints>
                        <constraint firstItem="Cy8-Tt-uZI" firstAttribute="leading" secondItem="6Fa-fa-e1T" secondAttribute="leading" id="R2l-Qe-Ovp"/>
                        <constraint firstItem="Szm-1V-GvH" firstAttribute="top" secondItem="Cy8-Tt-uZI" secondAttribute="bottom" id="TTP-dE-fVr"/>
                        <constraint firstItem="Cy8-Tt-uZI" firstAttribute="top" secondItem="6Fa-fa-e1T" secondAttribute="top" id="xee-OO-b7e"/>
                        <constraint firstAttribute="trailing" secondItem="Cy8-Tt-uZI" secondAttribute="trailing" id="zAr-p0-gYR"/>
                    </constraints>
                </view>
                <connections>
                    <outlet property="scrollView" destination="Cy8-Tt-uZI" id="Qqw-qu-YBE"/>
                    <outlet property="viewToMove" destination="2a3-Zk-7dR" id="Jjc-bt-n4N"/>
                </connections>
            </viewController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="Jhi-9S-UE4" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="1317.5999999999999" y="-497.00149925037488"/>
    </scene>


希望它对你有用

我已经创建了演示,对我来说,它工作得很好,这里是故事板
XML

  <!--View Controller Test-->
    <scene sceneID="Mea-LA-Bdd">
        <objects>
            <viewController id="apb-o6-bca" customClass="ViewControllerTest" customModule="StoryBoard" customModuleProvider="target" sceneMemberID="viewController">
                <layoutGuides>
                    <viewControllerLayoutGuide type="top" id="M5h-pq-oJT"/>
                    <viewControllerLayoutGuide type="bottom" id="Szm-1V-GvH"/>
                </layoutGuides>
                <view key="view" contentMode="scaleToFill" id="6Fa-fa-e1T">
                    <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <subviews>
                        <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Cy8-Tt-uZI">
                            <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                            <subviews>
                                <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="YI6-Cd-VzM">
                                    <rect key="frame" x="0.0" y="0.0" width="768" height="1792"/>
                                    <subviews>
                                        <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2a3-Zk-7dR">
                                            <rect key="frame" x="0.0" y="768" width="768" height="256"/>
                                            <color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
                                            <constraints>
                                                <constraint firstAttribute="height" constant="256" id="hFq-kg-mhd"/>
                                            </constraints>
                                        </view>
                                    </subviews>
                                    <color key="backgroundColor" red="0.87843137250000003" green="0.89411764709999997" blue="0.89411764709999997" alpha="1" colorSpace="calibratedRGB"/>
                                    <constraints>
                                        <constraint firstItem="2a3-Zk-7dR" firstAttribute="centerY" secondItem="YI6-Cd-VzM" secondAttribute="centerY" id="N66-2N-D4i"/>
                                        <constraint firstItem="2a3-Zk-7dR" firstAttribute="leading" secondItem="YI6-Cd-VzM" secondAttribute="leading" id="avT-2f-PKx"/>
                                        <constraint firstAttribute="trailing" secondItem="2a3-Zk-7dR" secondAttribute="trailing" id="nnJ-PW-ft4"/>
                                    </constraints>
                                </view>
                            </subviews>
                            <color key="backgroundColor" red="0.68627450980000004" green="0.72549019609999998" blue="0.74901960779999999" alpha="1" colorSpace="calibratedRGB"/>
                            <constraints>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="top" secondItem="Cy8-Tt-uZI" secondAttribute="top" id="C4r-zI-jfx"/>
                                <constraint firstAttribute="bottom" secondItem="YI6-Cd-VzM" secondAttribute="bottom" id="NDs-Z7-us0"/>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="height" secondItem="Cy8-Tt-uZI" secondAttribute="height" constant="768" id="NnT-HW-g61"/>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="width" secondItem="Cy8-Tt-uZI" secondAttribute="width" id="W7n-ym-257"/>
                                <constraint firstAttribute="trailing" secondItem="YI6-Cd-VzM" secondAttribute="trailing" id="bWQ-Fn-3rw"/>
                                <constraint firstItem="YI6-Cd-VzM" firstAttribute="leading" secondItem="Cy8-Tt-uZI" secondAttribute="leading" id="dTM-iV-SPc"/>
                            </constraints>
                            <edgeInsets key="layoutMargins" top="0.0" left="0.0" bottom="0.0" right="0.0"/>
                        </scrollView>
                    </subviews>
                    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                    <constraints>
                        <constraint firstItem="Cy8-Tt-uZI" firstAttribute="leading" secondItem="6Fa-fa-e1T" secondAttribute="leading" id="R2l-Qe-Ovp"/>
                        <constraint firstItem="Szm-1V-GvH" firstAttribute="top" secondItem="Cy8-Tt-uZI" secondAttribute="bottom" id="TTP-dE-fVr"/>
                        <constraint firstItem="Cy8-Tt-uZI" firstAttribute="top" secondItem="6Fa-fa-e1T" secondAttribute="top" id="xee-OO-b7e"/>
                        <constraint firstAttribute="trailing" secondItem="Cy8-Tt-uZI" secondAttribute="trailing" id="zAr-p0-gYR"/>
                    </constraints>
                </view>
                <connections>
                    <outlet property="scrollView" destination="Cy8-Tt-uZI" id="Qqw-qu-YBE"/>
                    <outlet property="viewToMove" destination="2a3-Zk-7dR" id="Jjc-bt-n4N"/>
                </connections>
            </viewController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="Jhi-9S-UE4" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="1317.5999999999999" y="-497.00149925037488"/>
    </scene>


希望它对你有用

我认为您应该改用UITableView(或UICollectionView)。很难控制通常的UIScrollView。您还可以设置UITableView.backgroundView=UIImageView(..)为什么不添加平移手势?看一看:您可以通过设置一个巨大的contentSize使您的scrollView滚动,而不管您的内部视图大小,并且将图像作为背景很容易。您可以将您的图像视图放在视图层次结构中滚动视图的后面,并将其固定在顶部、左侧和底部,在您的UIScrollViewWith autolayout右侧,我认为您不可能使用UITableView(或UICollectionView)。很难控制通常的UIScrollView。您还可以设置UITableView.backgroundView=UIImageView(..)为什么不添加平移手势?看一看:您可以通过设置一个巨大的contentSize使您的scrollView滚动,而不管您的内部视图大小,并且将图像作为背景很容易。您可以将您的图像视图放在视图层次结构中滚动视图的后面,并将其固定在顶部、左侧和底部,在您的UIScrollView的右侧,使用autolayout无法确定如何将图像作为背景,并且只能使黄色区域上下滚动?将图像设置为scrollview背景,或将scroll view背景颜色设置为清除,并将图像视图置于滚动视图下方,但如何将图像作为背景,以及