title: iOS Auto layout: Table View Cell width = 0
body: <p>I'm using Xcode 6.0.1 and for the first time I'm using auto layout on a <strong>Table View</strong> with <strong>Static Cells</strong>.</p>
  
  <p>I have a <strong>Text Field</strong> in the first table cell. This has a fixed height, but I want the width of the <strong>Text Field</strong> 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.</p>
  
  <p>I have added a height constraint (40 pixels), and leading and trailing space constraints (14 pixels) to the <strong>Text Field</strong>. I've also centered the Y alignment to the superview. None of these are <em>Relative to margin</em> since I want to target iOS 7 and I believe this option causes problems with that.</p>
  
  <p>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 <strong>Text Field</strong> 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).</p>
  
  <p>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.</p>
  
  <p>Here is my view hierarchy with the constrains on the <strong>Text Field</strong> (called <strong>Title</strong>) showing....</p>
  
  <p><img src="https://i.stack.imgur.com/7VLnl.png" alt="View hierarchy"></p>
  
  <p>And this is where I think the problem is, since the <em>Expected</em> height and width of the <strong>Table View Cell</strong> are both zero...</p>
  
  <p><img src="https://i.stack.imgur.com/Ww3s2.png" alt="Misplaced views"></p>
  
  <p>I've tried setting constraints on the <strong>Table View Cell</strong> etc to try and give it a non-zero width but this doesn't seem possible.</p>
  
  <hr>
  
  <p>Here is the first 40 lines or so of my .storyboard file. I haven't fiddled with the XML, it's straight from Xcode.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6245" systemVersion="14A379a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="3"&gt;
      &lt;dependencies&gt;
          &lt;plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/&gt;
          &lt;capability name="Constraints to layout margins" minToolsVersion="6.0"/&gt;
      &lt;/dependencies&gt;
      &lt;scenes&gt;
          &lt;!--Dev Note Edit Screen--&gt;
          &lt;scene sceneID="2"&gt;
              &lt;objects&gt;
                  &lt;tableViewController storyboardIdentifier="DevNoteEditScreen" id="3" customClass="DevNoteEditScreen" sceneMemberID="viewController"&gt;
                      &lt;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"&gt;
                          &lt;rect key="frame" x="0.0" y="0.0" width="320" height="568"/&gt;
                          &lt;autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/&gt;
                          &lt;color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/&gt;
                          &lt;sections&gt;
                              &lt;tableViewSection id="13"&gt;
                                  &lt;cells&gt;
                                      &lt;tableViewCell contentMode="scaleToFill" misplaced="YES" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="11"&gt;
                                          &lt;rect key="frame" x="0.0" y="134" width="375" height="44"/&gt;
                                          &lt;tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="11" id="12"&gt;
                                              &lt;rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/&gt;
                                              &lt;autoresizingMask key="autoresizingMask"/&gt;
                                              &lt;subviews&gt;
                                                  &lt;textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="Title" placeholder="Title" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="18"&gt;
                                                      &lt;constraints&gt;
                                                          &lt;constraint firstAttribute="height" constant="40" id="kkF-JV-vbs"/&gt;
                                                      &lt;/constraints&gt;
                                                      &lt;color key="textColor" red="0.16078431372549021" green="0.50196078431372548" blue="0.72549019607843135" alpha="1" colorSpace="calibratedRGB"/&gt;
                                                      &lt;fontDescription key="fontDescription" name="AvenirNext-DemiBold" family="Avenir Next" pointSize="19"/&gt;
                                                      &lt;textInputTraits key="textInputTraits" autocapitalizationType="sentences"/&gt;
                                                      &lt;connections&gt;
                                                          &lt;action selector="EditBegin:" destination="3" eventType="editingDidBegin" id="106"/&gt;
                                                          &lt;action selector="txtTitle_Changed:" destination="3" eventType="editingChanged" id="255"/&gt;
                                                      &lt;/connections&gt;
                                                  &lt;/textField&gt;
                                              &lt;/subviews&gt;
                                              &lt;constraints&gt;
                                                  &lt;constraint firstAttribute="trailing" secondItem="18" secondAttribute="trailing" constant="14" id="08w-eu-Siz"/&gt;
                                                  &lt;constraint firstItem="18" firstAttribute="leading" secondItem="12" secondAttribute="leading" constant="14" id="ImE-vD-cJD"/&gt;
                                                  &lt;constraint firstAttribute="centerY" secondItem="18" secondAttribute="centerY" id="hA5-l7-zX6"/&gt;
                                              &lt;/constraints&gt;
                                          &lt;/tableViewCellContentView&gt;
                                      &lt;/tableViewCell&gt;
  </code></pre>