ios - Auto Layout with UITextField causes exception -
i'd position uitextfield inside uiviewcontroller view using auto layout (dynamic resizing , 5 pt margin between textfield , superview). create view programmatically using loadview method.
header of uiviewcontroller
@property (weak, nonatomic) uitextfield *textfield;
implementation
-(void)loadview{ //frame uiview *frame = [[uiview alloc]initwithframe:cgrectmake(0, 0, 100, 40)]; [self setview:frame]; [self.view setbackgroundcolor:[uicolor redcolor]]; //textfield [self.textfield setdelegate:self]; [self.textfield settranslatesautoresizingmaskintoconstraints:no]; [self.textfield setbackgroundcolor:[uicolor bluecolor]]; [frame addsubview:self.textfield]; [frame addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-(5@50)-[textfield(==10@10)]-(5@50)-|" options:0 metrics:nil views:@{@"textfield" : self.textfield}]]; [frame addconstraints:[nslayoutconstraint constraintswithvisualformat: @"v:|-(5@50)-[textfield(==10@10)]-(5@50)-|" options:0 metrics:nil views:@{@"textfield" : self.textfield}]]; // //test uiview instead // uiview *sub = [[uiview alloc]init]; // [sub settranslatesautoresizingmaskintoconstraints:no]; // [sub setbackgroundcolor:[uicolor greencolor]]; // [frame addsubview:sub]; // // [frame addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-(5@50)-[sub(==10@10)]-(5@50)-|" // options:0 // metrics:nil // views:@{@"sub" : sub}]]; // // [frame addconstraints:[nslayoutconstraint constraintswithvisualformat: @"v:|-(5@50)-[sub(==10@10)]-(5@50)-|" // options:0 // metrics:nil // views:@{@"sub" : sub}]];
running throws exception. curiously using uiview ("sub") subview instead (the part commented out) work expected.
ouput:
shouldn't textfield behave uiview? how come textfield causes exception?
new finding: app crashes when use addconstraints on self.textfield. if remove 2 calls spare crash, textfield doesn't show either.
nslayoutconstraint part of ios 6. if try work code other ios6 ( such ios 5, ios 4.3) highly runtime error.
Comments
Post a Comment