xcode 无法让 UIImageView 使用自动布局填充父 UIView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26317636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get a UIImageView fill the parent UIView with autolayout
提问by OthmanT
I'm trying to fill a UIView with a UIImageView using auto layout. I've tried several thing, but the most obvious, equalSize with parent view only works when I set the correct size in "Simulated metric". If I let Freeform (the point of autolayout isn't it ?) I get messed up.
我正在尝试使用自动布局用 UIImageView 填充 UIView。我已经尝试了几件事,但最明显的,只有当我在“模拟指标”中设置正确的大小时,父视图的 equalSize 才有效。如果我让 Freeform (自动布局的重点不是吗?)我会搞砸。
I'm testing on an iPhone 4S and a 6 plus.
我正在 iPhone 4S 和 6 plus 上进行测试。
Thanks for any leading tracks.
感谢您提供任何领先的曲目。
EDIT FOR @mittens
编辑@mittens
I've seen your edit. I still can get it working. As you see I have 4 margin constraints, the same as in your code (I did not need the rest because I only use the main UIView). Anyway, when I change the size on xcode, the layout is perfect, when I send it to my 4S, I only get Top and left margins.
我看过你的编辑。我仍然可以让它工作。如您所见,我有 4 个边距约束,与您的代码相同(我不需要其余的,因为我只使用主 UIView)。无论如何,当我在xcode上更改大小时,布局是完美的,当我将其发送到我的4S时,我只会得到顶部和左边距。
回答by mittens
I'd double check the constraints on the UIView
you're trying to fill with the UIImageView
to make sure it is filling up its parent view as it should be when the storyboard/xib is set to freeform. Also if you're adding/creating the views programmatically double check that the views 'translatesAutoresizingMaskIntoConstraintsis set to
NO`. That always trips me up.
我会仔细检查UIView
你试图用 填充的约束,UIImageView
以确保它填充它的父视图,当故事板/xib 设置为自由格式时应该是这样。此外,如果您以编程方式添加/创建视图,请仔细检查视图“translatesAutoresizingMaskIntoConstraints is set to
NO”。那总是让我绊倒。
I made a super quick view and added some constraints to both a view inside the View Controllers view and an Image view to show one way to do it -- hopefully it helps at least a little
我做了一个超级快速的视图,并在 View Controllers 视图和 Image 视图中添加了一些约束来展示一种方法——希望它至少有一点帮助
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.backdropView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
self.backdropView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.5];
self.backdropView.translatesAutoresizingMaskIntoConstraints = NO; // Make sure this is set to NO if the view is being added programmatically
[self.view addSubview:self.backdropView]; // Always add the view into the hierarchy _before_ constraints are added (again, if creating & adding programmatically)
NSLayoutConstraint *backdropViewWidth = [NSLayoutConstraint constraintWithItem:self.backdropView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.backdropView.superview attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
NSLayoutConstraint *backdropViewHeight = [NSLayoutConstraint constraintWithItem:self.backdropView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.backdropView.superview attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0];
NSLayoutConstraint *backdropViewCenterX = [NSLayoutConstraint constraintWithItem:self.backdropView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.backdropView.superview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint *backdropViewCenterY = [NSLayoutConstraint constraintWithItem:self.backdropView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.backdropView.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[self.backdropView.superview addConstraints:@[backdropViewWidth, backdropViewHeight, backdropViewCenterY, backdropViewCenterX]];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
self.imageView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:1 alpha:0.5];
[self.backdropView addSubview:self.imageView];
NSLayoutConstraint *imageViewTop = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.imageView.superview attribute:NSLayoutAttributeTop multiplier:1 constant:8];
NSLayoutConstraint *imageViewLeft = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.imageView.superview attribute:NSLayoutAttributeLeft multiplier:1 constant:8];
NSLayoutConstraint *imageViewRight = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.imageView.superview attribute:NSLayoutAttributeRight multiplier:1 constant:-8];
NSLayoutConstraint *imageViewBottom = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.imageView.superview attribute:NSLayoutAttributeBottom multiplier:1 constant:-8];
[self.imageView.superview addConstraints:@[imageViewTop, imageViewLeft, imageViewRight, imageViewBottom]];
[self.view layoutIfNeeded];
}
That produces and
这产生和
Again, hopefully this helps.
再次,希望这会有所帮助。
EDIT: how to add them in storyboard
编辑:如何将它们添加到故事板中
Note on photo 3, I select the view I want to constrain and then shift
select the view I want to constraint it to. So I selected the inside UIView
(because it width/height will be constrained to the parent view) then shift
selected its parent view (the view it is nested inside of) to enable those options of Equal Width
& Equal Height
请注意照片 3,我选择了我想要约束shift
的视图,然后选择我想要将其约束为的视图。所以我选择了里面UIView
(因为它的宽度/高度将被限制到父视图)然后shift
选择它的父视图(它嵌套在里面的视图)以启用Equal Width
&Equal Height
Add constraints to UIImageView nested inside the UIView
Center UIView inside its parent view
Add width/height constraints equal to parent -- Note: I select the view I want to constrain and then
shift
select the view I want to constraint it to. So I selected the inside UIView
(because it width/height will be constrained to the parent view) then shift
selected its parent view (the view it is nested inside of) to enable those options of Equal Width
& Equal Height
Change multiplier in constraint to be whatever you want, 0.5 in this case
Celebrate
向嵌套在 UIView Center UIView 内的UIImageView 添加约束
在其父视图中
添加宽度/高度约束等于 parent -- 注意:我选择我想要约束
shift
的视图,然后选择我想要将它约束到的视图。所以我选择了内部UIView
(因为它的宽度/高度将被约束到父视图)然后shift
选择它的父视图(它嵌套在其中的视图)以启用Equal Width
&Equal Height
更改约束中的乘数的这些选项为您想要的任何值,0.5在这种情况下
庆祝
回答by Sampayo
i'm not sure but try this in your imageview.
我不确定,但在你的图像视图中试试这个。
[imgView clipToBounds:YES];
I hope it's work for you
我希望它对你有用