Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically set LayoutMargins for UITableViewCell - iOS 7 alternative

How do I set the margins (or insets, as they're called for iOS) in iOS 7 programatically?

In iOS 8, I set the margins using something like this:

cell.contentView.layoutMargins = UIEdgeInsets(
                    top: 0,
                    left: 6.5,
                    bottom: BOTTOM_MARGIN_FOR_CELLS,
                    right: 6.5)

But I want the app to retain backwards compatibility with iOS7.

I need to set the margins programmatically, because there is a condition where the top cell has a different margin from the other cells.

like image 633
Adé Avatar asked Mar 21 '26 06:03

Adé


1 Answers

For iOS7 use the following code

if ([myTableView respondsToSelector:@selector(separatorInset)]) {
    [myTableView setSeparatorInset:UIEdgeInsetsZero];
}

For iOS8 use the following code

if ([myTableView respondsToSelector:@selector(layoutMargins)]) {
    myTableView.layoutMargins = UIEdgeInsetsZero;
}

Note: Include both the code in your ViewController to support for both versions

like image 80
RStack Avatar answered Mar 23 '26 23:03

RStack