首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS自动布局:表视图单元格宽度=0

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

Stack Overflow用户
提问于 2014-10-23 17:03:01
回答 1查看 2.1K关注 0票数 1

我使用的是Xcode 6.0.1,这是我第一次在带有静态单元格的表视图上使用自动布局。

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

我向文本字段添加了一个高度约束(40个像素),以及引导和跟踪空间约束(14个像素)。我也把Y对齐到了superview。所有这些都与保证金无关,因为我想以iOS 7为目标,我认为这个选项会导致问题。

我的问题是,表单元格的宽度要变为零,这也导致我的文本字段的宽度为零,这是因为前面和后面的空间限制。如果我为我的文本字段设置了一个固定的宽度,那么一切都很好,但是它并没有利用iPhone 6上可用的额外宽度。当我实际运行这个应用程序时,我的表和表格单元格都在缩放,尽管它们在Xcode中显示为“错位”,它认为高度和宽度应该是零(见下面的屏幕快照)。

我在谷歌上搜索了无数次,也找不到其他有这些问题的人。缩放到表格单元格的高度是一个常见的问题,但不是宽度问题。

下面是我的视图层次结构,其中文本字段(称为Title)上的约束显示.

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

我尝试过在、表、视图、单元格、等上设置约束,尝试给它一个非零的宽度,但这似乎是不可能的。

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

代码语言:javascript
运行
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2014-10-23 19:04:42

奇怪的是,我通过编辑.storyboard文件XML并从<tableViewCell>元素中删除以下内容来修复这个问题.

代码语言:javascript
运行
复制
misplaced="YES"
translatesAutoresizingMaskIntoConstraints="NO"

并在<tableViewCell>元素中添加这个..。

代码语言:javascript
运行
复制
<autoresizingMask key="autoresizingMask"/>

好像我的故事板文件搞砸了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26533446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档