How to set multiplier for the UIImageView

╄→гoц情女王★ 提交于 2019-12-11 21:04:45

问题


I have an image whose width = 189, height = 41, so I defined 2 constraints for this UIImageView like this:

NSArray *constraint_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[Title(41)]" options:0 metrics:nil views:viewsDictionary];
[imgVwLogo addConstraints:constraint_V];

NSArray *constraint_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[Title(189)]" options:0 metrics:nil views:viewsDictionary];
[imgVwLogo addConstraints:constraint_H];

It looks good on iPhone 4s, but when it comes to iPhone 6 plus I feel the height of the UIImageview should be a bit increased, because the image looks shrunk vertically. Maybe adding a multiplier could be the solution. But I don't know how to select the multiplier factor for my elements. I set it as height/width

NSLayoutConstraint *imgtitlecon_Aspect_Ratio =[NSLayoutConstraint
                                          constraintWithItem:imgVwLogo
                                          attribute:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                          toItem:imgVwLogo
                                          attribute:NSLayoutAttributeHeight
                                          multiplier:41/189
                                          constant:0.0f];
[imgVwLogo addConstraint:imgtitlecon_Aspect_Ratio];

but this makes that UIImageView disappear entirely even on the 4s. How can I solve this problem?

UPDATE

  //------------------------ Title image --------------------------------------------

 NSArray *constraint_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:   [Title(41)]" options:0 metrics:nil views:viewsDictionary];
[imgVwLogo addConstraints:constraint_V];

NSArray *constraint_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[Title(189)]" options:0 metrics:nil views:viewsDictionary];
[imgVwLogo addConstraints:constraint_H];


// Center Horizontally
NSLayoutConstraint *centerXConstraintimgTitle =
[NSLayoutConstraint constraintWithItem:imgVwLogo
                             attribute:NSLayoutAttributeCenterX
                             relatedBy:NSLayoutRelationEqual
                                toItem:self.view
                             attribute:NSLayoutAttributeCenterX
                            multiplier:1.0
                              constant:0.0];
[self.view addConstraint:centerXConstraintimgTitle];


NSLayoutConstraint *imgtitlecon_Aspect_Ratio =[NSLayoutConstraint
                                          constraintWithItem:imgVwLogo
                                          attribute:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                          toItem:imgVwLogo
                                          attribute:NSLayoutAttributeHeight
                                          multiplier:(41.0f/189.0f)
                                          constant:0.0f];
[imgVwLogo addConstraint:imgtitlecon_Aspect_Ratio];




NSArray *Titleconstraint_POS_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-titleVspace-[Title]-titleVspace-|" options:0 metrics:metrics views:viewsDictionary];
[self.view addConstraints:Titleconstraint_POS_H];



//------------------------ Title image ------------------------------------------------------------

and finally I align my all elements

NSArray *btncon_POS_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[Title]-0-[vwgap]-0-[lblFirst]-0-[lblSecond]-20-[textusername]-10-[txtpassword]-20-[btnLogin]-0-[vwgapCopy]-0-[copyrightlbl]" options:0 metrics:nil views:viewsDictionary];

[self.view addConstraints:btncon_POS_V];

回答1:


If you just want to set aspect ratio from code then you just need to change a small thing in your code. Here it goes.

[imgVwLogo addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      attribute:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:imgVwLogo
                                      attribute:NSLayoutAttributeWidth
                                      multiplier:(41.0f/189.0f)
                                      constant:0.0f]];

EDIT 1: according to question edit the answer will be like below code. EDIT 2: please change the 568.0f value to the value of the self.view height at the time of designing in storyboard.

[imgVwLogo addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      attribute:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:imgVwLogo
                                      attribute:NSLayoutAttributeWidth
                                      multiplier:(41.0f/189.0f)
                                      constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      attribute:NSLayoutAttributeWidth
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeHeight
                                      multiplier:(189.0f/568.0f)
                                      constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      attribute:NSLayoutAttributeCenterX
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeCenterX
                                      multiplier:1.0f
                                      constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeTop
                                      multiplier:1.0f
                                      constant:50.0f]];


来源:https://stackoverflow.com/questions/31447014/how-to-set-multiplier-for-the-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!