gpt4 book ai didi

swift - UITableView 在框架外滚动

转载 作者:行者123 更新时间:2023-11-28 07:46:35 30 4
gpt4 key购买 nike

我有一个固定宽度的 UITableView。表格在其容器中居中,因此左右两侧都有白色边距。

是否也可以用手指在空白处滚动表格?

提前致谢!

编辑:根据马特的建议,我尝试使用平移手势识别器不幸的是,它工作起来不是那么顺畅,根本没有弹跳

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
self.tableView.setContentOffset(CGPoint(x: 0, y: self.tableView.contentOffset.y - translation.y), animated: true)
}

最佳答案

您可以将 tableview 放在 scrollview 中,并将 Clip to Bounds 设置为 false。下面共享 Storyboard 示例。

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina5_5" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<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="OptionalTest" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="zrT-93-180">
<rect key="frame" x="65" y="149" width="221" height="134"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<scrollView multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="TPs-fa-Cha">
<rect key="frame" x="20" y="20" width="374" height="429"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="LQo-x0-VDn">
<rect key="frame" x="0.0" y="0.0" width="414" height="429"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8PI-hl-lry">
<rect key="frame" x="-20" y="0.0" width="414" height="429"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="2v6-FK-vyt">
<rect key="frame" x="30" y="20" width="354" height="389"/>
<color key="backgroundColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" id="gZa-kH-87J">
<rect key="frame" x="0.0" y="28" width="354" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="gZa-kH-87J" id="Oid-Rc-aH5">
<rect key="frame" x="0.0" y="0.0" width="354" height="43.666666666666664"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Test" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dJP-b6-Ftm">
<rect key="frame" x="20" y="11" width="500" height="22"/>
<constraints>
<constraint firstAttribute="height" priority="750" constant="50" id="vcL-NN-bXq"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstAttribute="bottomMargin" secondItem="dJP-b6-Ftm" secondAttribute="bottom" id="DgB-3a-RR8"/>
<constraint firstItem="dJP-b6-Ftm" firstAttribute="leading" secondItem="Oid-Rc-aH5" secondAttribute="leadingMargin" id="Hli-JL-2Qr"/>
<constraint firstAttribute="trailingMargin" secondItem="dJP-b6-Ftm" secondAttribute="trailing" id="kia-ek-skG"/>
<constraint firstItem="dJP-b6-Ftm" firstAttribute="top" secondItem="Oid-Rc-aH5" secondAttribute="topMargin" id="l5N-zS-cxF"/>
</constraints>
</tableViewCellContentView>
</tableViewCell>
</prototypes>
</tableView>
</subviews>
<color key="backgroundColor" red="0.75004076959999999" green="0.1040449888" blue="0.1206566915" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="2v6-FK-vyt" secondAttribute="bottom" constant="20" id="Edy-hU-GY1"/>
<constraint firstAttribute="width" constant="414" id="Isx-E5-2uJ"/>
<constraint firstAttribute="trailing" secondItem="2v6-FK-vyt" secondAttribute="trailing" constant="30" id="OTY-td-pfO"/>
<constraint firstItem="2v6-FK-vyt" firstAttribute="leading" secondItem="8PI-hl-lry" secondAttribute="leading" constant="30" id="T7h-dc-4QT"/>
<constraint firstItem="2v6-FK-vyt" firstAttribute="top" secondItem="8PI-hl-lry" secondAttribute="top" constant="20" id="dcV-5c-pkc"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="8PI-hl-lry" secondAttribute="trailing" constant="20" id="018-wR-9xA"/>
<constraint firstItem="8PI-hl-lry" firstAttribute="leading" secondItem="LQo-x0-VDn" secondAttribute="leading" constant="-20" id="FKC-nR-YRp"/>
<constraint firstItem="8PI-hl-lry" firstAttribute="top" secondItem="LQo-x0-VDn" secondAttribute="top" id="Gqv-OU-P2f"/>
<constraint firstAttribute="bottom" secondItem="8PI-hl-lry" secondAttribute="bottom" id="KK5-wW-ozL"/>
</constraints>
</view>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="LQo-x0-VDn" secondAttribute="trailing" id="AE2-7E-9HG"/>
<constraint firstItem="LQo-x0-VDn" firstAttribute="width" secondItem="TPs-fa-Cha" secondAttribute="width" priority="250" id="Rte-o0-Glp"/>
<constraint firstItem="LQo-x0-VDn" firstAttribute="top" secondItem="TPs-fa-Cha" secondAttribute="top" id="aEU-v5-Cad"/>
<constraint firstAttribute="bottom" secondItem="LQo-x0-VDn" secondAttribute="bottom" id="cO7-2E-0pT"/>
<constraint firstItem="LQo-x0-VDn" firstAttribute="leading" secondItem="TPs-fa-Cha" secondAttribute="leading" id="m81-U4-SNF"/>
<constraint firstItem="LQo-x0-VDn" firstAttribute="height" secondItem="TPs-fa-Cha" secondAttribute="height" id="owm-Yu-tsT"/>
</constraints>
</scrollView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="EJ1-7L-gD8">
<rect key="frame" x="0.0" y="481" width="414" height="255"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9zS-iV-m0P">
<rect key="frame" x="0.0" y="0.0" width="414" height="127.66666666666667"/>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="lpS-nX-jsH">
<rect key="frame" x="0.0" y="127.66666666666663" width="414" height="127.33333333333331"/>
<color key="backgroundColor" cocoaTouchSystemColor="viewFlipsideBackgroundColor"/>
</view>
</subviews>
<constraints>
<constraint firstAttribute="height" constant="255" id="oGk-Vz-GWg"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="EJ1-7L-gD8" firstAttribute="bottom" secondItem="6Tk-OE-BBY" secondAttribute="bottom" id="EgJ-Wh-ZOP"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="EJ1-7L-gD8" secondAttribute="trailing" id="Srj-nB-74S"/>
<constraint firstItem="TPs-fa-Cha" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="frI-np-m0B"/>
<constraint firstItem="EJ1-7L-gD8" firstAttribute="top" secondItem="TPs-fa-Cha" secondAttribute="bottom" constant="32" id="jfk-9F-7WW"/>
<constraint firstItem="TPs-fa-Cha" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="20" id="pR9-do-d64"/>
<constraint firstItem="TPs-fa-Cha" firstAttribute="trailing" secondItem="6Tk-OE-BBY" secondAttribute="trailing" constant="-20" id="r3d-o9-LYw"/>
<constraint firstItem="EJ1-7L-gD8" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="vmi-E0-e52"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="label" destination="zrT-93-180" id="9YE-hX-UB7"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="139.13043478260872" y="132.06521739130434"/>
</scene>
</scenes>
</document>

关于swift - UITableView 在框架外滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50829133/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com