objective c - How to add UIbutton on UIview programmatically in ios using autolayouts -
i want add uibutton on uiview programmatically using autolayouts , tried code it's not working please me one
//adding uiview using autolayouts
uiview * myview; myview = [uiview new]; myview.translatesautoresizingmaskintoconstraints = no; myview.backgroundcolor = [uicolor colorwithred:0.95 green:0.47 blue:0.48 alpha:1.0]; [self.view addsubview:myview]; nslayoutconstraint * constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributeleading multiplier:1.0f constant:30.0f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributetop multiplier:1.0f constant:100.0f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem: nil attribute:nslayoutattributenotanattribute multiplier:1.0f constant:100.0f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem: nil attribute:nslayoutattributenotanattribute multiplier:1.0f constant:100.0f]; [self.view addconstraint:constraint];
adding button on uiview using autolayouts:-
mainbutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; [mainbutton settitle:@"mainbutton" forstate:uicontrolstatenormal]; [mainbutton sizetofit]; mainbutton.backgroundcolor = [uicolor blackcolor]; mainbutton.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:mainbutton]; nslayoutconstraint *constraint = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:myview attribute:nslayoutattributeleading multiplier:1.0f constant:30.f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:myview attribute:nslayoutattributetop multiplier:1.0f constant:30.f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributewidth relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:nslayoutattributewidth multiplier:1.0f constant:20.f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributeheight relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:nslayoutattributeheight multiplier:1.0f constant:20.f]; [self.view addconstraint:constraint];
but button not adding on myview please me one
this working :
uiview *myview = [uiview new]; myview.translatesautoresizingmaskintoconstraints = no; myview.backgroundcolor = [uicolor colorwithred:0.95 green:0.47 blue:0.48 alpha:1.0]; [self.view addsubview:myview]; nslayoutconstraint * constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributeleading multiplier:1.0f constant:30.0f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributetop multiplier:1.0f constant:100.0f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem: nil attribute:nslayoutattributenotanattribute multiplier:1.0f constant:140.0f]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:myview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem: nil attribute:nslayoutattributenotanattribute multiplier:1.0f constant:100.0f]; [self.view addconstraint:constraint]; uibutton *mainbutton = [uibutton new]; mainbutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; [mainbutton settitle:@"mainbutton" forstate:uicontrolstatenormal]; [mainbutton sizetofit]; mainbutton.backgroundcolor = [uicolor blackcolor]; mainbutton.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:mainbutton]; nslayoutconstraint *constraint1 = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:myview attribute:nslayoutattributeleading multiplier:1.0f constant:30.f]; [self.view addconstraint:constraint1]; constraint1 = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:myview attribute:nslayoutattributetop multiplier:1.0f constant:30.f]; [self.view addconstraint:constraint1]; constraint1 = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributewidth relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:nslayoutattributewidth multiplier:1.0f constant:20.f]; [self.view addconstraint:constraint1]; constraint1 = [nslayoutconstraint constraintwithitem:mainbutton attribute:nslayoutattributeheight relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:nslayoutattributeheight multiplier:1.0f constant:20.f]; [self.view addconstraint:constraint1];
Comments
Post a Comment