objective c - iOS 8 UITableView separator inset 0 not working -


i have app uitableview's separator inset set custom values - right 0, left 0. works in ios 7.x, in ios 8.0 see separator inset set default of 15 on right. though in xib files set 0, still shows incorrectly.

how remove uitableviewcell separator margins?

ios 8.0 introduces layoutmargins property on cells , table views.

this property isn't available on ios 7.0 need make sure check before assigning it!

additionally, apple has added property cell prevent inheriting table view's margin settings. when property set, cells allowed configure own margins independently of table view. think of override.

this property called preservessuperviewlayoutmargins, , setting no allow cell's layoutmargin setting override whatever layoutmargin set on tableview. both saves time (you don't have modify table view's settings), , more concise. please refer mike abdullah's answer detailed explanation.

note: follows clean implementation cell-level margin setting, expressed in mike abdullah's answer. setting cell's preservessuperviewlayoutmargins=no ensure table view not override cell settings. if want entire table view have consistent margins, please adjust code accordingly.

setup cell margins:

-(void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath {     // remove seperator inset     if ([cell respondstoselector:@selector(setseparatorinset:)]) {            [cell setseparatorinset:uiedgeinsetszero];     }      // prevent cell inheriting table view's margin settings     if ([cell respondstoselector:@selector(setpreservessuperviewlayoutmargins:)]) {         [cell setpreservessuperviewlayoutmargins:no];     }      // explictly set cell's layout margins     if ([cell respondstoselector:@selector(setlayoutmargins:)]) {         [cell setlayoutmargins:uiedgeinsetszero];     } } 

setting preservessuperviewlayoutmargins property on cell no should prevent table view overriding cell margins. in cases, seems not function properly.

if fails, may brute-force table view margins:

-(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];     } }  

...and there go! should work on ios 7 , 8.

edit: mohamed saleh brought attention possible change in ios 9. may need set table view's celllayoutmarginsfollowreadablewidth no if want customize insets or margins. mileage may vary, not documented well.

this property exists in ios 9 sure check before setting.

if([mytableview respondstoselector:@selector(setcelllayoutmarginsfollowreadablewidth:)]) {     mytableview.celllayoutmarginsfollowreadablewidth = no; }  

(above code ios 8 uitableview separator inset 0 not working)

edit: here's pure interface builder approach:

tableviewattributesinspector tableviewcellsizeinspector

note: ios 11 changes & simplifies of behavior, update forthcoming...


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -