如何设置乘数为的UIImageView [英] How to set multiplier for the UIImageView

查看:123
本文介绍了如何设置乘数为的UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形象的宽度= 189,高度= 41,所以我定义2约束这种的UIImageView 是这样的:

 的NSArray * constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@V:[标题(41)]选项:0的指标:无意见:viewsDictionary];
[imgVwLogo addConstraints:constraint_V];NSArray的* constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@H:[标题(189)]选项:0的指标:无意见:viewsDictionary];
[imgVwLogo addConstraints:constraint_H];

它看起来对iPhone 4S好,但是当涉及到​​iPhone 6加我觉得的UIImageView 应该有点增加,因为图像看起来垂直缩水的高度。
也许添加乘数可以是溶液。但我不知道该如何选择乘数因子为我的元素。我将它设置为高度/宽度

  NSLayoutConstraint * imgtitlecon_Aspect_Ratio = [NSLayoutConstraint
                                          constraintWithItem:imgVwLogo
                                          属性:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                          toItem:imgVwLogo
                                          属性:NSLayoutAttributeHeight
                                          事半功倍:一百八十九分之四十一
                                          恒:0.0];
[imgVwLogo addConstraint:imgtitlecon_Aspect_Ratio];

但是这使得该的UIImageView 完全消失,即使是在4S。我怎样才能解决这个问题?

更新

  // ------------------------标题图像---------- ---------------------------------- NSArray的* constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@V:[标题(41)]选项:0的指标:无意见:viewsDictionary];
[imgVwLogo addConstraints:constraint_V];NSArray的* constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@H:[标题(189)]选项:0的指标:无意见:viewsDictionary];
[imgVwLogo addConstraints:constraint_H];
//水平居中
NSLayoutConstraint * centerXCons​​traintimgTitle =
[NSLayoutConstraint constraintWithItem:imgVwLogo
                             属性:NSLayoutAttributeCenterX
                             relatedBy:NSLayoutRelationEqual
                                toItem:self.view
                             属性:NSLayoutAttributeCenterX
                            事半功倍:1.0
                              恒:0.0];
[self.view addConstraint:centerXCons​​traintimgTitle];
NSLayoutConstraint * imgtitlecon_Aspect_Ratio = [NSLayoutConstraint
                                          constraintWithItem:imgVwLogo
                                          属性:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                          toItem:imgVwLogo
                                          属性:NSLayoutAttributeHeight
                                          乘数:(41.0f / 189.0f)
                                          恒:0.0];
[imgVwLogo addConstraint:imgtitlecon_Aspect_Ratio];
NSArray的* Titleconstraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@H:| -titleVspace- [标题] -titleVspace- |选项​​:0指标:指标的观点:viewsDictionary];
[self.view addConstraints:Titleconstraint_POS_H];// ------------------------标题图像---------------------- --------------------------------------

最后我调整我的所有元素

 的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]\"选项​​:0的指标:无意见:viewsDictionary];[self.view addConstraints:btncon_POS_V];


解决方案

如果你只是想设置从code宽高比,那么你只需要改变你的code一件小事情。在这里,它去。

  [imgVwLogo addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      属性:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:imgVwLogo
                                      属性:NSLayoutAttributeWidth
                                      乘数:(41.0f / 189.0f)
                                      恒:0.0];

编辑1:根据问题编辑答案会像下面code。
编辑2:请在故事板设计时改变568.0f价值的self.view高度值

  [imgVwLogo addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      属性:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:imgVwLogo
                                      属性:NSLayoutAttributeWidth
                                      乘数:(41.0f / 189.0f)
                                      恒:0.0];
[self.view addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      属性:NSLayoutAttributeWidth
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      属性:NSLayoutAttributeHeight
                                      乘数:(189.0f / 568.0f)
                                      恒:0.0];
[self.view addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      属性:NSLayoutAttributeCenterX
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      属性:NSLayoutAttributeCenterX
                                      事半功倍:1.0F
                                      恒:0.0];
[self.view addConstraint:[NSLayoutConstraint
                                      constraintWithItem:imgVwLogo
                                      属性:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      属性:NSLayoutAttributeTop
                                      事半功倍:1.0F
                                      常量:50.0f]];

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];

解决方案

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]];

这篇关于如何设置乘数为的UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆