ios - AutoLayout programmatically cause issue in iphone 4 -
i learning auto layout code. learning purpose doing sample apps using autolayout. in sample app decided keep text field below 20 points top margin. here code
#import "sampleviewcontroller.h" @interface sampleviewcontroller (){ uitextfield *username; } @end @implementation sampleviewcontroller - (void)viewdidload { [super viewdidload]; self.edgesforextendedlayout = uirectedgenone; [self prepareviews]; } -(void)prepareviews{ username = [[uitextfield alloc]init]; username.placeholder = @"username"; [username setborderstyle:uitextborderstyleroundedrect]; username.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:username]; [self prepareconstraint]; } -(void)prepareconstraint{ nslayoutconstraint *constraint = [nslayoutconstraint constraintwithitem:username attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecenterx multiplier:1.0 constant:0]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:username attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1.0 constant:250]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:username attribute:nslayoutattributetopmargin relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributetopmargin multiplier:1.0 constant:20]; [self.view addconstraint:constraint]; } @end
when run code on iphone 4(ios 7.1) got output when run code on iphone 5s(ios 8.4) got output
guys don't know making mistake.kindly point out me making mistake.and got warning when run code on iphone 4 (ios 7.1) .
unable simultaneously satisfy constraints. @ least 1 of constraints in following list 1 don't want. try this: (1) @ each constraint , try figure out don't expect; (2) find code added unwanted constraint or constraints , fix it. (note: if you're seeing nsautoresizingmasklayoutconstraints don't understand, refer documentation uiview property translatesautoresizingmaskintoconstraints) ( "<nslayoutconstraint:0x14dcac70 h:|-(20)-[uitextfield:0x14d955b0] (names: '|':uiview:0x14d9b480 )>" )
will attempt recover breaking constraint <nslayoutconstraint:0x14dcac70 h:|-(20)-[uitextfield:0x14d955b0]
(names: '|':uiview:0x14d9b480 )>break on objc_exception_throw catch in debugger. methods in uiconstraintbasedlayoutdebugging category on uiview listed in <uikit/uiview.h> may helpful.
this tested in 4s , 5s....you add width constraint view rather should apply textfield...
nslayoutconstraint *constraint = [nslayoutconstraint constraintwithitem:username attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecenterx multiplier:1.0 constant:0]; [self.view addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:username attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1.0 constant:250]; [username addconstraint:constraint]; constraint = [nslayoutconstraint constraintwithitem:username attribute:nslayoutattributetopmargin relatedby:nslayoutrelationequal toitem:self.toplayoutguide attribute:nslayoutattributebottom multiplier:1.0 constant:20]; [self.view addconstraint:constraint];
Comments
Post a Comment