【问题标题】:iOS Swift ScrollView, UITableView, scroll areaiOS Swift ScrollView、UITableView、滚动区域
【发布时间】:2017-06-22 08:48:30
【问题描述】:

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

【问题讨论】:

  • 当向上滚动导航栏到达中间部分时,这3个标签保持在顶部。我不明白。给我看一张你想要的照片。
  • 您是否为此实现了代码?或者只是在开始实施之前询问。
  • 我还没找到办法,所以才问
  • @elk_cloner: 请检查第二张图片,这就是我想要的

标签: ios swift uitableview uiscrollview


【解决方案1】:

我刚刚写了以下代码。为了方便起见,我做了一些 stroyboard 设置。有什么问题可以问我。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var darkGrayView: UIView!
    @IBOutlet weak var grayView: UIView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var redViewTopConstrain: NSLayoutConstraint! 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        darkGrayView.backgroundColor = .darkGray
        grayView.backgroundColor = .gray
        tableView.delegate = self
        tableView.dataSource = self
        automaticallyAdjustsScrollViewInsets = false

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell?.textLabel?.text = "Let's try"
        return cell!
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 12
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        if  redViewTopConstrain.constant != -60{
            UIView.animate(withDuration: 1, animations: {
                self.redViewTopConstrain.constant = -60
                self.view.layoutIfNeeded()
            })
        }
    }
}

更新: Storyboard源码(右键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>

【讨论】:

  • 哦,是的,这正是我想要的。你能分享故事板文件吗,这样我就可以看到你设置的约束了吗?
  • @HoangTrung 每次编辑我的答案(甚至粘贴、复制)时,我的 chrome 浏览器都会崩溃。实际上,我写了一个没有 Storyboard 的 SnapKit 版本。不知道怎么更新,因为太长放在这里当评论了。
  • 感谢您的代码。我跑了,但这不是我想要的:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 2018-12-12
  • 2021-01-04
  • 1970-01-01
相关资源
最近更新 更多