【问题标题】:UITableViewCell loaded with wrong traits variationUITableViewCell 加载了错误的特征变化
【发布时间】:2020-12-04 15:37:45
【问题描述】:

我有一个 UITableViewCell,其中包含一个 UICollectionView,iPhone 和 iPad 之间的显示略有不同。

因此,iPhone (CR) 安装了一些约束,但 iPad (RR) 未安装。 似乎当tableViewCell显示时,它是我的xib最后保存的状态,我的意思是:

  • 如果我在 iPad 上查看 xib 时保存它,我的 tableview 将在 iPad 上正确显示,但在 iPhone 上不正确
  • 如果我在 iPhone 上查看 xib 时保存它,我的 tableView 将在 iPhone 上正确显示,但不能在 iPad 上显示

但是,如果我向下滚动以隐藏单元格并向上滚动,布局就可以了。 希望我已经足够清楚了。我错过了什么?

【问题讨论】:

  • 尝试在您的cellForItem 中调用layoutIfNeeded
  • 不工作,我也在 willDisplayCell 中尝试过
  • 是的,我很害怕。也许您将无法使用此情节提要功能;您必须自己在代码中进行“不同”布局。我在执行自动布局的表格视图和集合视图单元格方面遇到了各种问题。正如你的问题所暗示的,这里的时间似乎有问题。

标签: ios swift uitableview constraints interface-builder


【解决方案1】:

在 cellForItem 中尝试 layoutIfNeeded() 和 layoutSubViews(),

也许将高度设置为 800 并使用边距 left right top left 0?

【讨论】:

    【解决方案2】:

    特征变体应该在 xib 中使用表格视图单元格时可以正常工作。

    不过,如果您不经常使用它们,可能会造成混淆。

    试试这个...

    • 创建一个空的xib
    • 添加表格视图单元格
    • 添加标签(带背景色即可查看)

    将标签限制在顶部:0 / 前导:0 / 底部:0 的边距,并将其宽度限制为 160:

    现在,选择 Width 约束,在 Size Inspector 窗格中,单击 Constant 左侧的 + 符号,然后选择 Width: RegularHeight: Any

    ...然后点击Add Variation:

    现在将新特征变体的 Constant 值更改为 600

    这是它在 iPhone 8 上的外观:

    在 iPad Pro 9.7 上:

    为了玩它,这是我使用的代码:

    class TraitCell: UITableViewCell {
        @IBOutlet var theLabel: UILabel!
    }
    
    class TraitTestTableViewController: UITableViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.register(UINib(nibName: "TraitCell", bundle: nil), forCellReuseIdentifier: "traitCell")
        }
        override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 20
        }
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let c = tableView.dequeueReusableCell(withIdentifier: "traitCell", for: indexPath) as! TraitCell
            c.theLabel.text = "\(indexPath)"
            return c
        }
    }
    

    这是TraitCell.xib的来源:

    <?xml version="1.0" encoding="UTF-8"?>
    <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
        <device id="retina4_7" orientation="portrait" appearance="light"/>
        <dependencies>
            <deployment identifier="iOS"/>
            <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17505"/>
            <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
        </dependencies>
        <objects>
            <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
            <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
            <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" rowHeight="48" id="Lnk-lV-Xmf" customClass="TraitCell" customModule="DelMe" customModuleProvider="target">
                <rect key="frame" x="0.0" y="0.0" width="414" height="48"/>
                <autoresizingMask key="autoresizingMask"/>
                <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Lnk-lV-Xmf" id="A7R-fE-SaJ">
                    <rect key="frame" x="0.0" y="0.0" width="414" height="48"/>
                    <autoresizingMask key="autoresizingMask"/>
                    <subviews>
                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vB6-W5-Xl5">
                            <rect key="frame" x="16" y="11" width="160" height="26"/>
                            <color key="backgroundColor" red="0.98977929350000005" green="0.91474151609999999" blue="0.16292566059999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            <constraints>
                                <constraint firstAttribute="width" constant="160" id="9U8-ws-UNn">
                                    <variation key="widthClass=regular" constant="600"/>
                                </constraint>
                            </constraints>
                            <fontDescription key="fontDescription" type="system" pointSize="17"/>
                            <nil key="textColor"/>
                            <nil key="highlightedColor"/>
                        </label>
                    </subviews>
                    <constraints>
                        <constraint firstItem="vB6-W5-Xl5" firstAttribute="top" secondItem="A7R-fE-SaJ" secondAttribute="topMargin" id="5GP-0t-6Zx"/>
                        <constraint firstItem="vB6-W5-Xl5" firstAttribute="leading" secondItem="A7R-fE-SaJ" secondAttribute="leadingMargin" id="REV-Ua-5gO"/>
                        <constraint firstAttribute="bottomMargin" secondItem="vB6-W5-Xl5" secondAttribute="bottom" id="cF3-NH-NX1"/>
                    </constraints>
                </tableViewCellContentView>
                <connections>
                    <outlet property="theLabel" destination="vB6-W5-Xl5" id="G7V-WZ-215"/>
                </connections>
                <point key="canvasLocation" x="344" y="185.30734632683661"/>
            </tableViewCell>
        </objects>
    </document>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多