Tuesday 15 July 2014

ios - how to set auto layout for dynamically created buttons? -



ios - how to set auto layout for dynamically created buttons? -

creating dynamic buttons , labels scroll view want set auto layout . hows possible set multiple dynamic buttons .i search many tutorial not got illustration set auto layout multiple dynamic buttons.its work buttons set .means show 1 button , label. search show right result auto layout not work . problem

-(void)dynamicbutton:(nsmutablearray*)objectname { for(uiview *view in [scrollview subviews]) { [view removefromsuperview]; } int ypossion = 100, xpossion = 44; int temp = 0; (int = 0; i<[objectname count]; i++) { smscategory *cat = [objectname objectatindex:i]; uibutton *abutton = [uibutton buttonwithtype:uibuttontypecustom]; [abutton settag:i]; [abutton settranslatesautoresizingmaskintoconstraints:yes]; [abutton setbackgroundcolor:[uicolor blackcolor]]; [abutton setbackgroundimage:[uiimage imagenamed:@"icon-menu.png"] forstate:uicontrolstatenormal]; [abutton settitle:[nsstring stringwithformat:@"%d",i] forstate:uicontrolstatenormal]; [abutton setframe:cgrectmake(xpossion, ypossion, 70, 60)]; abutton.highlighted=yes; [scrollview addsubview:abutton]; ; xpossion += abutton.frame.size.width+35; temp++; if (temp==3) { ypossion = abutton.frame.origin.y+abutton.frame.size.height+20; temp = 0; xpossion = 44; ypossion += abutton.frame.size.width-15; [scrollview setcontentsize:cgsizemake(scrollview.frame.size.width ,ypossion- 50)]; } uilabel *label = [[uilabel alloc] init]; [label settranslatesautoresizingmaskintoconstraints:yes]; [label settext:cat.name]; [label settextcolor:[uicolor blackcolor]]; label.font = [uifont systemfontofsize:12]; [label sizetofit]; [label setframe:cgrectmake(4, 44, 70, 60)]; [scrollview addsubview:label]; [abutton addsubview:label]; } } //autolayout code [abutton settranslatesautoresizingmaskintoconstraints:no]; nsdictionary *viewsdictionary = @{@"abutton":abutton}; // 2. define button sizes nsarray *abutton_constraint_h = [nslayoutconstraint constraintswithvisualformat:@"v:[abutton(60)]" options:0 metrics:nil views:viewsdictionary]; nsarray *abutton_constraint_v = [nslayoutconstraint constraintswithvisualformat:@"h:[abutton(70)]" options:0 metrics:nil views:viewsdictionary]; [abutton addconstraints:abutton_constraint_h]; [abutton addconstraints:abutton_constraint_v]; // 3. define views positions using options nsarray *constraint_pos_v = [nslayoutconstraint constraintswithvisualformat:@"v:|-80-[abutton]" options:0 metrics:nil views:viewsdictionary]; nsarray *constraint_pos = [nslayoutconstraint constraintswithvisualformat:@"h:|-20-[abutton]" options:0 metrics:nil views:viewsdictionary]; [self.view addconstraints:constraint_pos_v]; [self.view addconstraints:constraint_pos]; }

you need add together constraints programmatically, similar how have searchbar code. did re-create code somewhere without understanding it?

there many many tutorials this, such as: https://medium.com/@jsleeuw/mastering-programmatic-auto-layout-b02ed2499d79

you need create autolayout constraints this:

[self.view addconstraint:[nslayoutconstraint constraintwithitem:label attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecenterx multiplier:1.0 constant:0.0]];

there visual format have sample of in code.

i edited question clean text , format code readable. please note changes made create easier people read code.

ios objective-c ios-autolayout

No comments:

Post a Comment