簡體   English   中英

iOS自動布局:表格視圖單元格寬度= 0

[英]iOS Auto layout: Table View Cell width = 0

我正在使用Xcode 6.0.1,並且第一次是在具有靜態單元格表視圖上使用自動布局。

我在第一個表格單元格中有一個文本字段 它具有固定的高度,但我希望“ 文本字段”的寬度與表(或表單元格,因為它們的寬度相同)的寬度成比例。 這是因為我想利用iPhone 6和6 Plus的額外寬度。

我在Text Field中添加了高度限制(40像素)以及前導和尾隨空間約束(14像素)。 我也將Y對齊方式對准了超級視圖。 這些都不是相對於保證金的,因為我想針對iOS 7,並且我相信此選項會導致問題。

我的問題是表格單元格的寬度想要變為零,並且由於前導和尾隨的空間限制,這也導致我的文本字段的寬度也變為零。 如果我為“ 文本字段”設置了固定的寬度,那么一切都很好,但是它並沒有利用iPhone 6上可用的額外寬度。我實際運行應用程序時,表格和表格單元格可以很好地縮放,盡管它們在Xcode中顯示為“放錯位置”,認為高度和寬度應為零(請參見下面的屏幕截圖)。

我已經無休止地搜尋了Google,但找不到其他遇到這些問題的人。 縮放到表格單元格的高度是一個常見問題,而不是寬度。

這是我的視圖層次結構,其中顯示了“ 文本字段” (稱為“ 標題” )上的約束。

視圖層次

這就是我認為的問題所在,因為Table View單元期望高度和寬度都為零...

錯位的意見

我嘗試在Table View Cell等上設置約束,以嘗試為其提供非零寬度,但這似乎是不可能的。


這是我的.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>

奇怪的是,我設法通過編輯.storyboard文件XML並從<tableViewCell>元素中刪除了以下內容來解決此問題...

misplaced="YES"
translatesAutoresizingMaskIntoConstraints="NO"

並將其添加到<tableViewCell>元素中...

<autoresizingMask key="autoresizingMask"/>

似乎我的故事板文件因某種原因變得混亂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM