objective c - floating UIButton with image on a UITableview -


this question has answer here:

i try implement floating button on uitableview. when content scrolls button stay @ same position. button shall hold transparent image.

i did smoketest , worked fine insertsubview:abovesubview:respectivly addsubview

but doing on uitableview of tableviewcontroller not seem work. button on tableview , scrolls tableview. interestingly scrolls under header of tableview...

how fix - button floating on tableview?

tableviewcontroller.m (updated working code 12.07.2013)

edit: added @property (nonatomic) float originalorigin; in tableviewcontroller.h

- (void)viewdidload {     [super viewdidload];            self.originalorigin = 424.0;     [self addoverlaybutton]; }   #pragma mark camera button  - (void)addoverlaybutton {     // uibutton *obutton = [uibutton buttonwithtype:uibuttontyperoundedrect];     self.obutton = [uibutton buttonwithtype:uibuttontypecustom];     self.obutton.frame = cgrectmake(132.0, self.originalorigin, 56.0, 56.0);     self.obutton.backgroundcolor = [uicolor clearcolor];     [self.obutton setimage:[uiimage imagenamed:@"camerabutton56x56.png"] forstate:uicontrolstatenormal];     [self.obutton addtarget:self action:@selector(tocameraview:) forcontrolevents:uicontroleventtouchdown];     [self.view insertsubview:self.obutton abovesubview:self.view]; }  // make button float on tableview including tableheader - (void)scrollviewdidscroll:(uiscrollview *)scrollview {     cgrect tablebounds = self.tableview.bounds;     cgrect floatingbuttonframe = self.obutton.frame;     floatingbuttonframe.origin.y = self.originalorigin + tablebounds.origin.y;     self.obutton.frame = floatingbuttonframe;      [self.view bringsubviewtofront:self.obutton]; // float on tableheader } 

you can implement scrollviewdidscroll delegate change floating button y origin while user scroll table, button float , stay @ same location.

there great example in wwdc 2011 session 125 want.

in general, need save original y origin of button after create it, or can in viewdidload, implement scrollviewdidscroll , update button frame.

- (void)scrollviewdidscroll:(uiscrollview *)scrollview {     cgrect tablebounds = self.tableview.bounds;     cgrect floatingbuttonframe = self.floatingbutton.frame;     floatingbuttonframe.origin.y = self.originalorigin + tablebounds.origin.y;     self.floatingbutton.frame = floatingbuttonframe; } 

Comments