ios - How do I horizontally center the UIView in my UITableView section footer? -
i'm adding either or down arrow image in uitableview section's footer so:
uiview *view = [[uiview alloc] initwithframe:cgrectzero]; view.backgroundcolor = [uicolor whitecolor];  morebutton *morebutton = [morebutton buttonwithtype:uibuttontypecustom]; morebutton.frame = cgrectmake(0, 0, 60, 22); morebutton.sectionindex = section;  if (self.expandedsection == -1){     [morebutton setimage:[uiimage imagenamed:@"downarrow.png"] forstate:uicontrolstatenormal]; } else {     if (self.expandedsection == section) {         [morebutton setimage:[uiimage imagenamed:@"uparrow.png"] forstate:uicontrolstatenormal];     }     else {         [morebutton setimage:[uiimage imagenamed:@"downarrow.png"] forstate:uicontrolstatenormal];     }  }  [morebutton addtarget:self action:@selector(morebuttonselected:) forcontrolevents:uicontroleventtouchupinside]; [view addsubview:morebutton];  return view;  this works fine, except fact has image positioned way on left, while want in middle:
i know because of positioning gave frame (cgrectmake(0, 0, 60, 22);), i'm not sure how set x coordinate have horizontally centered, regardless of screen size. 
this line:
morebutton.frame = cgrectmake(0, 0, 60, 22); says want button appear @ origin 0,0 of it's superview (the super view being footer).
if change like:
morebutton.frame = cgrectmake(200, 0, 60, 22); it'll closer middle.
to right, need use auto layout.
based on the code question, should able like:
morebutton *morebutton = [morebutton buttonwithtype:uibuttontypecustom]; morebutton.frame = cgrectmake(0, 0, 60, 22); morebutton.sectionindex = section;  [morebutton settranslatesautoresizingmaskintoconstraints:no]; [self.view addsubview:viewobj];  nslayoutconstraint* cn = [nslayoutconstraint constraintwithitem:morebutton                                                       attribute:nslayoutattributecenterx                                                       relatedby:nslayoutrelationequal                                                          toitem:self.view                                                       attribute:nslayoutattributecenterx                                                       multiplier:1.0                                                        constant:0]; [self.view addconstraint:cn];  // centers y... not sure if need or not cn = [nslayoutconstraint constraintwithitem:morebutton                                    attribute:nslayoutattributecentery                                   relatedby:nslayoutrelationequal                                       toitem:self.view                                   attribute:nslayoutattributecentery                                  multiplier:1.0                                    constant:0]; [self.view addconstraint:cn]; 
Comments
Post a Comment