我有一个宽度= 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];
在iPhone 4s上看起来很不错,但是当涉及iPhone 6 plus时,我觉得UIImageview
的高度应该稍微增加,因为图像看起来垂直缩小。
也许添加乘数可能是解决方案。但我不知道如何为我的元素选择乘数因子。我将其设置为高度/宽度
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];
但是这使UIImageView
完全消失,甚至在4s。我该如何解决这个问题?
更新
//------------------------ 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 ------------------------------------------------------------
最后我调整了所有元素
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];
答案 0 :(得分:0)
如果您只想从代码中设置宽高比,那么您只需要在代码中更改一些小东西。在这里。
[imgVwLogo addConstraint:[NSLayoutConstraint
constraintWithItem:imgVwLogo
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:imgVwLogo
attribute:NSLayoutAttributeWidth
multiplier:(41.0f/189.0f)
constant:0.0f]];
编辑1:根据问题编辑,答案将如下面的代码。 编辑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]];