使一个UIView组件可上下滚动

时间:2017-08-30 13:18:32

标签: ios uiview uiscrollview

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

enter image description here

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

我现在陷入困境,我不确定自己是否正朝着实现目标的方向前进。有人可以指导我走向正确的方向吗?

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我已经创建了demo,对我而言,这里的工作正常是故事板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>

希望它对你有用..