如何为 UIImageView 设置乘数
How to set multiplier for the UIImageView
我有一个图像,其宽度 = 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];
在iPhone4s上看起来不错,但是到了iPhone6plus我觉得UIImageview
的高度应该增加一点,因为图像看起来缩小了垂直。
也许添加乘数可能是解决方案。但是我不知道如何 select 我的元素的乘数。我设置为 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];
但这使得 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];
如果您只想从代码中设置宽高比,那么您只需要在代码中做一些小改动。开始了。
[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]];
我有一个图像,其宽度 = 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];
在iPhone4s上看起来不错,但是到了iPhone6plus我觉得UIImageview
的高度应该增加一点,因为图像看起来缩小了垂直。
也许添加乘数可能是解决方案。但是我不知道如何 select 我的元素的乘数。我设置为 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];
但这使得 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];
如果您只想从代码中设置宽高比,那么您只需要在代码中做一些小改动。开始了。
[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]];