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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With