我有一个像附加图像的视图,顶部是图像&标签,中间部分是3个标签,底部是表格视图。我希望在向上滑动时,即使用户触摸桌面视图进行滑动,整个视图也会向上移动,而不仅仅是桌面视图内容。当向上滚动并且导航栏到达3个选项卡时,这3个选项卡保持在顶部,并且tableview将继续滚动它的内容。如下图所示。我怎样才能做到这一点?
答案 0 :(得分:0)
我刚刚编写了以下代码。为方便起见,我做了一些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()
})
}
}
}
<强>更新强> 故事板的源代码(右键单击Stroyboard文件 - &gt;打开 - &gt;源代码):
<?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>