[英]How to set multiplier for the UIImageView
I have an image whose width = 189, height = 41, so I defined 2 constraints for this UIImageView
like this: 我有一个图像,其宽度= 189,高度= 41,因此我为此
UIImageView
定义了2个约束,如下所示:
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. 在iPhone 4s上看起来不错,但是在iPhone
UIImageview
上,我觉得UIImageview
的高度应该增加一点,因为图像在垂直方向上会缩小。 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. 但这使得
UIImageView
即使在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. 编辑1:根据问题编辑答案将类似于以下代码。 EDIT 2: please change the 568.0f value to the value of the self.view height at the time of designing in storyboard.
编辑2:在情节提要中进行设计时,请将568.0f值更改为self.view高度的值。
[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]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.