0

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];
IRD
  • 1,127
  • 2
  • 8
  • 15

1 Answers1

0

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]];
Mahesh Agrawal
  • 3,348
  • 20
  • 34
  • I set like this. But still it dissapearing from the screen. Do I need to set any other thing? – IRD Jul 16 '15 at 06:56
  • How can I select the correct multiplier value for my UIImageview – IRD Jul 16 '15 at 07:10
  • applying only one constraint is not sufficient...edit your question with a sample project in github and i will apply constraint to what you needed. – Mahesh Agrawal Jul 16 '15 at 07:22
  • for this situation if you are applying aspect ratio only then you need either width or height constraint, you need vertical alignment constraint and for this either you assign top or bottom or vertical center, you need horizontal alignment and for this either you assign left or right or horizontal center. – Mahesh Agrawal Jul 16 '15 at 07:25
  • I updated the question with my all constraint for that UIImageView. Can you check and tell me – IRD Jul 16 '15 at 08:13
  • Thanks,, It makes the same size the UIImageview in all screens. But Can I increase the width and size proportionally when it comes to larger screens without streching the UIImage? – IRD Jul 16 '15 at 10:02