ios - A better way to implement margins in a UITableView? -
apple has deprecated margins in table views, since ios7, designers still seem want margins. what’s best way t implement margins in ios 7 , above? old “group” table view style included margins in ios 6 no longer indents left , right edges.
i have auitableview
subclass, in i’m using following method programmatically add margins edge of subviews, except thebackgroundview
(such cells, section header etc):
code:
- (void)layoutsubviews { [super layoutsubviews]; (uiview *subview in self.subviews) { if (subview != self.backgroundview && subview.frame.origin.x == 0.0 && subview.frame.size.width == self.frame.size.width) { subview.frame = cgrectinset(subview.frame, 10.0, 0); } } }
during runtime, seems work desired. cells resize according margin offset , subviews of cells adjust correctly.
however, in storyboard, usingibdesignable
, cells adjust correctly cells’ subviews not seem resize within cell’s calculated size
is there better way implement margins in auitableview
?
thanks c:
uiedgeinsets used implement margins around tableview looks grouped tableview. may try below solutions , check it.
-(void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath{ if ([tableview respondstoselector:@selector(setseparatorinset:)]) { [tableview setseparatorinset:uiedgeinsetszero];//ios7 } if ([tableview respondstoselector:@selector(setlayoutmargins:)]) { [tableview setlayoutmargins:uiedgeinsetszero]; } if ([cell respondstoselector:@selector(setlayoutmargins:)]) { [cell setlayoutmargins:uiedgeinsetszero]; } }
//or
-(void)viewdidlayoutsubviews { [super viewdidlayoutsubviews]; // force tableview margins (this may bad idea) if ([self.tableview respondstoselector:@selector(setseparatorinset:)]) { [self.tableview setseparatorinset:uiedgeinsetszero]; } if ([self.tableview respondstoselector:@selector(setlayoutmargins:)]) { [self.tableview setlayoutmargins:uiedgeinsetszero]; } }
for ios 8 use
- (uiedgeinsets)layoutmargins { return uiedgeinsetszero; }
hope helps you..
Comments
Post a Comment