ios - Aligning a UIImageView with Auto Layout -


what want add image subview, align centered along x axis , 10 points bottom of superview. need use auto layout only, , preferably visual formatting language.

- (void)viewdidload {     [super viewdidload];      self.imageview = [[uiimageview alloc] initwithframe:cgrectzero];     [self.imageview settranslatesautoresizingmaskintoconstraints:no];     [self.imageview setimage:[uiimage imagenamed:@"06-arrow-south"]];     self.imageview.contentmode = uiviewcontentmodescaleaspectfit;     [self.view addsubview:self.imageview];     [self addconstraints];      self.imageview.layer.bordercolor = [[uicolor redcolor] cgcolor];      self.imageview.layer.borderwidth = 1.0; }  - (void)addconstraints {      nsdictionary *viewsdictionary = @{@"arrowimage":self.imageview};      [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-[arrowimage(==40)]-|"                                                                  options:0                                                                  metrics:nil                                                                    views:viewsdictionary]];      [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-[arrowimage(==40)]-10-|"                                                                  options:0                                                                  metrics:nil                                                                    views:viewsdictionary]]; } 

here's i'm getting:

enter image description here

v:|-[arrowimage]-10-| 

this aligns image view standard length (20pt) top of superview, , 10 bottom. want pin bottom only:

v:[arrowimage]-10-| 

i'm not sure centering in superview can done visual format, can create single constraint center it:

[self.view addconstraint:  [nslayoutconstraint   constraintwithitem:self.imageview   attribute:nslayoutattributecenterx   relatedby:nslayoutrelationequal   toitem:self.view   attribute:nslayoutattributecenterx   multiplier:1   constant:0]]; 

there's no need set height or width of image view; size determined content.

so, here's full code addconstraints method:

- (void)addconstraints {      [self.view addconstraint:      [nslayoutconstraint       constraintwithitem:self.imageview       attribute:nslayoutattributecenterx       relatedby:nslayoutrelationequal       toitem:self.view       attribute:nslayoutattributecenterx       multiplier:1       constant:0]];      nsdictionary *viewsdictionary = @{@"arrowimage":self.imageview};      [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:[arrowimage]-10-|"                                                                  options:0                                                                  metrics:nil                                                                    views:viewsdictionary]]; } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -