UITableViewに追加したカスタムスタイルセルのコンテンツ左右の余白が標準スタイルのそれよりも若干狭くて表示がずれるという現象を何とかしたお話です。
標準スタイルで構成
標準スタイルのセルを3つ追加してみました。
![img20160315_1-1](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_1-1.png?resize=525%2C298)
これを実行するとiPhone 6、iPhone 6 Plus、iPadではこうなります。
![img20160315_1-2](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_1-2.png?resize=337%2C600)
![img20160315_1-3](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_1-3.png?resize=338%2C600)
![img20160315_1-4](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_1-4.png?resize=450%2C600)
標準スタイルとカスタムスタイルの混合
さて、2行目のセルをカスタムスタイルに変更してラベルとスイッチを適当に置いてみます。
![img20160315_2-1](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_2-1.png?resize=525%2C298)
Auto Layoutのマージン制約はXcodeのお勧めをそのまま設定します。
![img20160315_2-2](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_2-2.png?resize=525%2C298)
![img20160315_2-3](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_2-3.png?resize=525%2C298)
これを実行するとiPhone 6、iPhone 6 Plus、iPadではこうなります。
![img20160315_2-4](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_2-4.png?resize=337%2C600)
![img20160315_2-5](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_2-5.png?resize=338%2C600)
![img20160315_2-6](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_2-6.png?resize=450%2C600)
iPhone 6 PlusとiPadでは、なぜか2行目の左右余白が少ないため、Cell 2と書かれたラベルが左に、スイッチが右に若干ずれてしまっています。iPhone 6とiPhone 6 PlusやiPadで異なる余白のシステム設定値が、カスタムセルの方にだけ正しく継承されていないようです。
カスタムスタイルの余白修正
ビューのプロパティをいじって継承されるように修正してみます。
大事なところは、セルの親ビューをたどってPreserve Superview Marginsにチェックを入れて、さらにラベルとスイッチのレイアウト制約にある親ビュー側のアイテムのRelative to marginをオンにすることです。
![img20160315_3-1](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-1.png?resize=525%2C298)
![img20160315_3-2](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-2.png?resize=525%2C298)
![img20160315_3-3](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-3.png?resize=525%2C298)
![img20160315_3-4](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-4.png?resize=525%2C298)
![img20160315_3-5](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-5.png?resize=525%2C298)
これを実行するとiPhone 6、iPhone 6 Plus、iPadではこうなります。
![img20160315_3-6](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-6.png?resize=337%2C600)
![img20160315_3-7](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-7.png?resize=338%2C600)
![img20160315_3-8](https://i0.wp.com/ishiweb.hio.homeunix.net/wp-content/uploads/2016/03/img20160315_3-8.png?resize=450%2C600)
iPhone 6 Plus、iPadでの表示のずれがなくなりました。相変わらずAuto Layoutは黒魔術です。