3

I designed a sophisticated UITableViewCell in Interface Builder, which has many subviews and constraints. The problem is that they many of them are direct children of the content view. I now want to insert a UIView between the content view and all of my subviews.

Or, another way to think about this is to imagine I want to include all of my cell's content on a normal UIViewController without a table view.

What is the easiest way to do this without having to manually recreate all of my constraints?

Senseful
  • 86,719
  • 67
  • 308
  • 465
  • What is your actual reason for wanting to do this? You can (if what you mentioned is what you really want to do) remove the contentView from the cell, and add it to another view. – rdelmar Sep 13 '14 at 20:26

2 Answers2

6

I am not sure if anyone has written this solution, but the solution is a bit hacky. If you want to convert UITableViewCell contentView into basic view, you have to follow these steps:

  1. Open storyboard as Source Code as XML file. You can use some XML editor to help you during editing this file
  2. Find array of <tableViewCell> elements in storyboard XML file
  3. Inside <tableViewCell> tag find <tableViewCellContentView> element. Copy its content somewhere inside XML file (the best idea is <subviews> element to avoid some errors during editing XML file)
  4. Convert <tableViewCellContentView key="contentView opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" to <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO". I believe that there are some possible differences in generated XML in this step, but the principle is obvious. Just make an empty view to check the valid XML settings for the empty view.
  5. Convert </tableViewCellContentView> to </view>

After those steps, you will have your view ready for editing inside storyboard, and you can delete the original cell from the table if you want to.

Vladimir88dev
  • 733
  • 1
  • 10
  • 19
5

Is pretty easy, you just need to select all of your views and in the upper menu choose EDITOR->EMBED IN->VIEW
The only issue is that it will add a sort of padding of 20pts. Check this answer for further details

Community
  • 1
  • 1
Andrea
  • 26,120
  • 10
  • 85
  • 131