iOS Swift ScrollView、UITableView、滚动区域

iOS Swift ScrollView、UITableView、滚动区域,ios,swift,uitableview,uiscrollview,Ios,Swift,Uitableview,Uiscrollview,我有一个类似附加图像的视图,顶部是图像和标签,中间是3个选项卡,底部是表格视图。我希望在向上滑动时,即使用户触摸表视图进行滑动,整个视图也会向上移动,而不仅仅是表视图内容。当向上滚动并且导航栏到达3个选项卡时,这3个选项卡保持在顶部,tableview将继续滚动其内容。如下图所示。我怎样才能做到这一点 我刚刚编写了以下代码。为了方便,我做了一些stroyboard设置。如果有任何问题,请问我。 更新: 故事板的源代码(右键单击Stroyboard文件->在中打开->源代码): <?xml


我有一个类似附加图像的视图,顶部是图像和标签,中间是3个选项卡,底部是表格视图。我希望在向上滑动时,即使用户触摸表视图进行滑动,整个视图也会向上移动,而不仅仅是表视图内容。当向上滚动并且导航栏到达3个选项卡时,这3个选项卡保持在顶部,tableview将继续滚动其内容。如下图所示。我怎样才能做到这一点

我刚刚编写了以下代码。为了方便,我做了一些stroyboard设置。如果有任何问题,请问我。

更新: 故事板的源代码(右键单击Stroyboard文件->在中打开->源代码):

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="643-Rb-BwK">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
        <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="Test1" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <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>
                            <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" translatesAutoresizingMaskIntoConstraints="NO" id="e2z-6e-7xi">
                                <rect key="frame" x="0.0" y="184" width="375" height="483"/>
                                <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
                                <prototypes>
                                    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="cell" id="dUE-aw-Mel">
                                        <rect key="frame" x="0.0" y="55.5" width="375" height="44"/>
                                        <autoresizingMask key="autoresizingMask"/>
                                        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="dUE-aw-Mel" id="ogF-HR-Apy">
                                            <rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
                                            <autoresizingMask key="autoresizingMask"/>
                                        </tableViewCellContentView>
                                    </tableViewCell>
                                </prototypes>
                            </tableView>
                            <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="6Ey-Bf-14y">
                                <rect key="frame" x="0.0" y="124" width="375" height="60"/>
                                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                            </view>
                            <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="PrH-WT-w4c">
                                <rect key="frame" x="0.0" y="64" width="375" height="60"/>
                                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                <constraints>
                                    <constraint firstAttribute="height" constant="60" id="p1E-cJ-38m"/>
                                </constraints>
                            </view>
                        </subviews>
                        <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="height" secondItem="PrH-WT-w4c" secondAttribute="height" id="2W6-Id-tzd"/>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="top" secondItem="PrH-WT-w4c" secondAttribute="bottom" id="O0X-Kb-jHD"/>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="SIG-nu-NdA"/>
                            <constraint firstItem="e2z-6e-7xi" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="b8G-6I-bN9"/>
                            <constraint firstItem="e2z-6e-7xi" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" id="cTJ-6L-ngk"/>
                            <constraint firstItem="PrH-WT-w4c" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="mvJ-P9-whS"/>
                            <constraint firstItem="PrH-WT-w4c" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="nFF-iq-SDG"/>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" id="ojH-MK-ZEU"/>
                            <constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="e2z-6e-7xi" secondAttribute="bottom" id="qJ5-Lp-moa"/>
                            <constraint firstItem="PrH-WT-w4c" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" id="rF1-Tx-GkV"/>
                            <constraint firstItem="e2z-6e-7xi" firstAttribute="top" secondItem="6Ey-Bf-14y" secondAttribute="bottom" id="w8E-1f-Qbp"/>
                        </constraints>
                    </view>
                    <navigationItem key="navigationItem" id="hAA-f7-INh"/>
                    <connections>
                        <outlet property="darkGrayView" destination="PrH-WT-w4c" id="qyF-Mv-VrY"/>
                        <outlet property="grayView" destination="6Ey-Bf-14y" id="Xh9-V9-Ug9"/>
                        <outlet property="redViewTopConstrain" destination="mvJ-P9-whS" id="AAY-Lx-heP"/>
                        <outlet property="tableView" destination="e2z-6e-7xi" id="K3I-Th-fqz"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="1056.8" y="122.78860569715144"/>
        </scene>
        <!--Navigation Controller-->
        <scene sceneID="RVN-Qw-5As">
            <objects>
                <navigationController automaticallyAdjustsScrollViewInsets="NO" id="643-Rb-BwK" sceneMemberID="viewController">
                    <toolbarItems/>
                    <navigationBar key="navigationBar" contentMode="scaleToFill" id="f1e-bq-GKK">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
                        <autoresizingMask key="autoresizingMask"/>
                    </navigationBar>
                    <nil name="viewControllers"/>
                    <connections>
                        <segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="VrF-oh-acv"/>
                    </connections>
                </navigationController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="rQX-j1-VaE" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="117.59999999999999" y="776.76161919040487"/>
        </scene>
    </scenes>
</document>


当向上滚动,导航栏到达中间部分时,这3个选项卡保持在顶部。我不明白。给我看一张你希望它是什么样子的图片。检查这个。你实现了这个的代码吗?或者只是在开始实施之前问一下。我还没有找到一种方法来实现这一点,这就是我为什么要这么做的原因asked@elk_cloner:请检查第二个附加图像,这就是我想要的。哦,是的,这正是我想要的。你愿意共享故事板文件吗,这样我就可以看到你设置的约束条件了?@HoangTrung每次我编辑我的答案(甚至粘贴、复制)时,我的chrome浏览器都会崩溃。实际上,我写了一个没有故事板的SnapKit版本。不知道如何更新,因为它太长了,不能放在这里作为注释。谢谢你的代码。我运行了它,但这不是我想要的:(
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="643-Rb-BwK">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
        <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="Test1" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <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>
                            <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" translatesAutoresizingMaskIntoConstraints="NO" id="e2z-6e-7xi">
                                <rect key="frame" x="0.0" y="184" width="375" height="483"/>
                                <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
                                <prototypes>
                                    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="cell" id="dUE-aw-Mel">
                                        <rect key="frame" x="0.0" y="55.5" width="375" height="44"/>
                                        <autoresizingMask key="autoresizingMask"/>
                                        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="dUE-aw-Mel" id="ogF-HR-Apy">
                                            <rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
                                            <autoresizingMask key="autoresizingMask"/>
                                        </tableViewCellContentView>
                                    </tableViewCell>
                                </prototypes>
                            </tableView>
                            <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="6Ey-Bf-14y">
                                <rect key="frame" x="0.0" y="124" width="375" height="60"/>
                                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                            </view>
                            <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="PrH-WT-w4c">
                                <rect key="frame" x="0.0" y="64" width="375" height="60"/>
                                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                <constraints>
                                    <constraint firstAttribute="height" constant="60" id="p1E-cJ-38m"/>
                                </constraints>
                            </view>
                        </subviews>
                        <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="height" secondItem="PrH-WT-w4c" secondAttribute="height" id="2W6-Id-tzd"/>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="top" secondItem="PrH-WT-w4c" secondAttribute="bottom" id="O0X-Kb-jHD"/>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="SIG-nu-NdA"/>
                            <constraint firstItem="e2z-6e-7xi" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="b8G-6I-bN9"/>
                            <constraint firstItem="e2z-6e-7xi" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" id="cTJ-6L-ngk"/>
                            <constraint firstItem="PrH-WT-w4c" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="mvJ-P9-whS"/>
                            <constraint firstItem="PrH-WT-w4c" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="nFF-iq-SDG"/>
                            <constraint firstItem="6Ey-Bf-14y" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" id="ojH-MK-ZEU"/>
                            <constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="e2z-6e-7xi" secondAttribute="bottom" id="qJ5-Lp-moa"/>
                            <constraint firstItem="PrH-WT-w4c" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" id="rF1-Tx-GkV"/>
                            <constraint firstItem="e2z-6e-7xi" firstAttribute="top" secondItem="6Ey-Bf-14y" secondAttribute="bottom" id="w8E-1f-Qbp"/>
                        </constraints>
                    </view>
                    <navigationItem key="navigationItem" id="hAA-f7-INh"/>
                    <connections>
                        <outlet property="darkGrayView" destination="PrH-WT-w4c" id="qyF-Mv-VrY"/>
                        <outlet property="grayView" destination="6Ey-Bf-14y" id="Xh9-V9-Ug9"/>
                        <outlet property="redViewTopConstrain" destination="mvJ-P9-whS" id="AAY-Lx-heP"/>
                        <outlet property="tableView" destination="e2z-6e-7xi" id="K3I-Th-fqz"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="1056.8" y="122.78860569715144"/>
        </scene>
        <!--Navigation Controller-->
        <scene sceneID="RVN-Qw-5As">
            <objects>
                <navigationController automaticallyAdjustsScrollViewInsets="NO" id="643-Rb-BwK" sceneMemberID="viewController">
                    <toolbarItems/>
                    <navigationBar key="navigationBar" contentMode="scaleToFill" id="f1e-bq-GKK">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
                        <autoresizingMask key="autoresizingMask"/>
                    </navigationBar>
                    <nil name="viewControllers"/>
                    <connections>
                        <segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="VrF-oh-acv"/>
                    </connections>
                </navigationController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="rQX-j1-VaE" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="117.59999999999999" y="776.76161919040487"/>
        </scene>
    </scenes>
</document>