iOS自动布局:表格视图单元格宽度= 0

时间:2021-04-15 22:23:23

I'm using Xcode 6.0.1 and for the first time I'm using auto layout on a Table View with Static Cells.

我正在使用Xcode 6.0.1,这是我第一次使用静态单元格在Table View上使用自动布局。

I have a Text Field in the first table cell. This has a fixed height, but I want the width of the Text Field to scale with the width of the table (or the table cell, since these are the same width). This is because I want to take advantage of the extra width of the iPhone 6 and 6 Plus.

我在第一个表格单元格中有一个文本字段。这有一个固定的高度,但我希望文本字段的宽度与表的宽度(或表格单元格,因为它们是相同的宽度)一致。这是因为我想利用iPhone 6和6 Plus的额外宽度。

I have added a height constraint (40 pixels), and leading and trailing space constraints (14 pixels) to the Text Field. I've also centered the Y alignment to the superview. None of these are Relative to margin since I want to target iOS 7 and I believe this option causes problems with that.

我在文本字段中添加了一个高度约束(40像素),以及前导和尾随空间约束(14个像素)。我也将Y对齐集中在superview上。这些都不是相对于保证金,因为我想要针对iOS 7,我相信这个选项会导致问题。

My problem is that the width of the table cell wants to go to zero, and this is causing the width of my text field to go to zero as well, because of the leading and trailing space constraints. If I set a fixed width for my Text Field then everything is fine, but then it doesn't take advantage of the extra width available on an iPhone 6. My table and the table cells are scaling fine when I actually run the app, although they are showing as "misplaced" in Xcode which thinks the height and width should be zero (see screenshot below).

我的问题是表格单元格的宽度想要变为零,这也导致我的文本字段的宽度也变为零,因为前导和尾随空间约束。如果我为我的文本字段设置了固定的宽度,那么一切都很好,但是它没有利用iPhone 6上可用的额外宽度。我实际运行应用程序时,我的表格和表格单元格正在缩放。它们在Xcode中显示为“错位”,认为高度和宽度应为零(见下面的截图)。

I've Googled this no end and I can't find anyone else who has had these issues. Scaling to the height of a table cell is a common issue, but not the width.

我用谷歌搜索了这个结局,我找不到其他有这些问题的人。缩放到表格单元格的高度是一个常见问题,但不是宽度。

Here is my view hierarchy with the constrains on the Text Field (called Title) showing....

这是我的视图层次结构,文本字段上的约束(称为标题)显示....

iOS自动布局:表格视图单元格宽度= 0

And this is where I think the problem is, since the Expected height and width of the Table View Cell are both zero...

这就是我认为问题所在,因为表视图单元的预期高度和宽度均为零......

iOS自动布局:表格视图单元格宽度= 0

I've tried setting constraints on the Table View Cell etc to try and give it a non-zero width but this doesn't seem possible.

我已经尝试在Table View Cell等上设置约束来尝试给它一个非零宽度,但这似乎不可能。


Here is the first 40 lines or so of my .storyboard file. I haven't fiddled with the XML, it's straight from Xcode.

这是我的.storyboard文件的前40行左右。我没有摆弄XML,它直接来自Xcode。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6245" systemVersion="14A379a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="3">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
        <capability name="Constraints to layout margins" minToolsVersion="6.0"/>
    </dependencies>
    <scenes>
        <!--Dev Note Edit Screen-->
        <scene sceneID="2">
            <objects>
                <tableViewController storyboardIdentifier="DevNoteEditScreen" id="3" customClass="DevNoteEditScreen" sceneMemberID="viewController">
                    <tableView key="view" opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="11" sectionFooterHeight="11" id="4">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                        <sections>
                            <tableViewSection id="13">
                                <cells>
                                    <tableViewCell contentMode="scaleToFill" misplaced="YES" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="11">
                                        <rect key="frame" x="0.0" y="134" width="375" height="44"/>
                                        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="11" id="12">
                                            <rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
                                            <autoresizingMask key="autoresizingMask"/>
                                            <subviews>
                                                <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="Title" placeholder="Title" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="18">
                                                    <constraints>
                                                        <constraint firstAttribute="height" constant="40" id="kkF-JV-vbs"/>
                                                    </constraints>
                                                    <color key="textColor" red="0.16078431372549021" green="0.50196078431372548" blue="0.72549019607843135" alpha="1" colorSpace="calibratedRGB"/>
                                                    <fontDescription key="fontDescription" name="AvenirNext-DemiBold" family="Avenir Next" pointSize="19"/>
                                                    <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
                                                    <connections>
                                                        <action selector="EditBegin:" destination="3" eventType="editingDidBegin" id="106"/>
                                                        <action selector="txtTitle_Changed:" destination="3" eventType="editingChanged" id="255"/>
                                                    </connections>
                                                </textField>
                                            </subviews>
                                            <constraints>
                                                <constraint firstAttribute="trailing" secondItem="18" secondAttribute="trailing" constant="14" id="08w-eu-Siz"/>
                                                <constraint firstItem="18" firstAttribute="leading" secondItem="12" secondAttribute="leading" constant="14" id="ImE-vD-cJD"/>
                                                <constraint firstAttribute="centerY" secondItem="18" secondAttribute="centerY" id="hA5-l7-zX6"/>
                                            </constraints>
                                        </tableViewCellContentView>
                                    </tableViewCell>

1 个解决方案

#1


0  

Weirdly I managed to fix this by editing the .storyboard file XML and removing the following from the <tableViewCell> element...

奇怪的是,我设法通过编辑.storyboard文件XML并从 元素中删除以下内容来解决这个问题...

misplaced="YES"
translatesAutoresizingMaskIntoConstraints="NO"

And adding this inside the <tableViewCell> element...

并在 元素中添加它...

<autoresizingMask key="autoresizingMask"/>

It seems that my storyboard file became messed up somehow.

似乎我的故事板文件以某种方式搞砸了。

#1


0  

Weirdly I managed to fix this by editing the .storyboard file XML and removing the following from the <tableViewCell> element...

奇怪的是,我设法通过编辑.storyboard文件XML并从 元素中删除以下内容来解决这个问题...

misplaced="YES"
translatesAutoresizingMaskIntoConstraints="NO"

And adding this inside the <tableViewCell> element...

并在 元素中添加它...

<autoresizingMask key="autoresizingMask"/>

It seems that my storyboard file became messed up somehow.

似乎我的故事板文件以某种方式搞砸了。