Skip to content

Instantly share code, notes, and snippets.

@patridge
Last active August 29, 2015 14:15
Show Gist options
  • Save patridge/e70ecdd0d6e055d5b59b to your computer and use it in GitHub Desktop.
Save patridge/e70ecdd0d6e055d5b59b to your computer and use it in GitHub Desktop.
UITableView and UITableViewCell without the iOS 7/8 margins.
// LICENSE: MIT
public class UITableViewWithoutLayoutMargins : UITableView {
void InitializeWithoutMargins() {
// iOS 7
if (RespondsToSelector(new Selector("setSeparatorInset:"))) {
SeparatorInset = UIEdgeInsets.Zero;
}
// iOS 8[+?]
if (RespondsToSelector(new Selector("setLayoutMargins:"))) {
LayoutMargins = UIEdgeInsets.Zero;
}
}
public UITableViewWithoutLayoutMargins(CGRect frame, UITableViewStyle style) : base(frame, style) {
InitializeWithoutMargins();
}
public UITableViewWithoutLayoutMargins(CGRect frame) : base(frame) {
InitializeWithoutMargins();
}
public UITableViewWithoutLayoutMargins() : base() {
InitializeWithoutMargins();
}
}
public class UITableViewCellWithoutLayoutMargin : UITableViewCell {
void InitializeWithoutMargins() {
// iOS 8[+?]
if (RespondsToSelector(new Selector("setLayoutMargins:"))) {
LayoutMargins = UIEdgeInsets.Zero;
}
}
public UITableViewCellWithoutLayoutMargin(CGRect frame) : base(frame) {
InitializeWithoutMargins();
}
public UITableViewCellWithoutLayoutMargin(UITableViewCellStyle style, string reuseIdentifier) : base(style, reuseIdentifier) {
InitializeWithoutMargins();
}
public UITableViewCellWithoutLayoutMargin() : base() {
InitializeWithoutMargins();
}
}
// Usage: inherit from the base classes above.
// (Alternative use: just copy the initialize code in your own custom class constructors.)
public class YourMarginlessTableView : UITableViewWithoutLayoutMargins {
//...
}
public class YourMarginlessTableViewCell : UITableViewCellWithoutLayoutMargins {
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment