uibutton - How to add constraints programmatically in ios -
i created ui elements programmatically in ios. fine in iphone sices buttons streched.
btnlogin=[[uibutton alloc] initwithframe:cgrectmake(80, h-90, w-160, 40)]; way icreated button. know in interface builder can use auto layout. how can add constraints button programmatically?
because button width changing according screen size although left , right side edges fixed. how can avoide element streatching according device , want manage same gap between left , right side between button , parent view border.
how can this? please me thanks
this want
uibutton * button = [[uibutton alloc] init]; button.backgroundcolor = [uicolor redcolor]; [self.view addsubview:button]; nsdictionary * buttondic = nsdictionaryofvariablebindings(button); button.translatesautoresizingmaskintoconstraints = no; nsarray * hconstraints = [nslayoutconstraint constraintswithvisualformat:@"h:|-80-[button]-80-|" options:0 metrics:nil views:buttondic]; [self.view addconstraints:hconstraints]; nsarray * vconstraints = [nslayoutconstraint constraintswithvisualformat:@"v:[button(40)]-90-|" options:0 metrics:nil views:buttondic]; [self.view addconstraints:vconstraints]; screenshot iphone 6

iphone 6p

update:
h:|-80-[button]-80-|,this create constraints @ horizontal,button left , right superview keeps 80
"v:[button(40)]-90-|",this create constraints @ vertical,button height keeps 40 , bottoms superview bottoms keeps 90
Comments
Post a Comment