67

This is not a question, rather a solution to the problem I faced.

In Xcode 7, when the application is run on iOS 9 on iPad devices, the UITableViewCell leaves some margin onto the left side of the UITableView. And rotating the device to the landscape would increase the margins.

The solution I found is:

Setting cellLayoutMarginsFollowReadableWidth to NO.

self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;

This property is only available in iOS 9. So, you will have to put a condition to check the iOS version, else it will crash.

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_1)
{
    self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;
}
Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
stuti
  • 751
  • 1
  • 5
  • 7
  • [Have a same problem](http://stackoverflow.com/questions/31537196/ios-9-uitableview-separators-significant-left-margin) – Julian Jul 21 '15 at 11:38
  • Yes, this works perfectly for me especially on iPad with iOS 9. – Adrian Hoe Oct 05 '15 at 14:55
  • I just encountered a serious problem with this answer, it uses version `NSFoundationVersionNumber_iOS_8_1`, it needs to use `NSFoundationVersionNumber_iOS_8_4` instead, otherwise it will crash the app running iOS8.2 and iOS8.3 and IOS8.4 – Alex Jan 14 '16 at 16:13
  • I use to get my system version with `float version = [[[UIDevice currentDevice] systemVersion] floatValue];` so you can easily compare then with `if (version >= 9.0)` (and so prevent potential crash due to further updates of the same iOS version) – zbMax Jan 19 '16 at 14:24
  • 1
    See http://stackoverflow.com/questions/32845075/ios-9-uitableview-cells-text-label-not-the-full-width-of-the-uitableview for a slightly better approach , detecting functionality rather than iOS9 version number – Bradley Thomas Mar 11 '16 at 16:48

4 Answers4

39

iOS 9 and above:

This is because of a new feature called readable content guides. It provides margins that are suitable for reading. So, on iPhone and portrait iPad they are very small margins. But in the landscape, iPad they are bigger. In iOS 9, UITableViewCell margins default to following the readable content guide.

If you want to stop that, just set the tableView's cellLayoutMarginsFollowReadableWidth to NO/false.

Source: https://forums.developer.apple.com/thread/5496

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • 2
    Very nice answer! Regret that it is not yet documented. That question occurred while I was preparing [mine](http://stackoverflow.com/questions/31537196/ios-9-uitableview-separators-insets-significant-left-margin/) – Julian Jul 21 '15 at 11:52
13

Up to iOS 9

In viewDidLoad

Objective-C

- (void)viewDidLoad {
    [super viewDidLoad];
    //Required for iOS 9
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
        self.testTableView.cellLayoutMarginsFollowReadableWidth = NO;
    }
}

Swift

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 9.0, *) {
        tableViewDiet.cellLayoutMarginsFollowReadableWidth = false
    }
}

In TableViewDelegate methods add following code:

Objective-C

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

Swift

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    // Remove seperator inset
    if cell.respondsToSelector(Selector("setSeparatorInset:")) {
        cell.separatorInset = UIEdgeInsetsZero
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
        cell.preservesSuperviewLayoutMargins = false
    }

    // Explictly set your cell's layout margins
    if cell.respondsToSelector(Selector("setLayoutMargins:")) {
        cell.layoutMargins = UIEdgeInsetsZero
    }
}
Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
Bhuvan Bhatt
  • 3,276
  • 2
  • 18
  • 23
  • 1
    This did not work for me. That is why I had to look for some other approach. – stuti Jul 21 '15 at 11:12
  • 1
    This is not working with iOS 9. Have you tested it with iOS 9 and Xcode 7 with properly set command line tools in the preferences? – Julian Jul 21 '15 at 11:53
  • If you are using swift you should be using `if #available(iOS 9.0, *)` condition check. – GoodSp33d Oct 07 '15 at 05:38
3

I hope this is helpful.

if #available(iOS 9.0, *) {
      myTableView.cellLayoutMarginsFollowReadableWidth = false
}
Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
TomCobo
  • 2,886
  • 3
  • 25
  • 43
0

readableContentGuide is a layout guide that is already added to every UIView.

This is to ensure the user doesn't have to turn his head to read the content.

If you want to follow the readable content guide, then do the following:

let baseSection = UIView()

contentView.addSubview(baseSection)

baseSection.translatesAutoresizingMaskIntoConstraints = false

let insets = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)

baseSection.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor, constant: insets.left).isActive = true
baseSection.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor, constant: -insets.right).isActive = true
baseSection.topAnchor.constraint(equalTo: contentView.topAnchor, constant: insets.top).isActive = true
baseSection.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -insets.bottom).isActive = true

Note: In the code above the bottom and top anchors use the contentView instead of the readableContentGuide so that the content vertical margins change based on the tableView.rowHeight.

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
user1046037
  • 16,755
  • 12
  • 92
  • 138