简体   繁体   English

无法通过autolayout获取UIImageView填充父UIView

[英]Can't get a UIImageView fill the parent UIView with autolayout

I'm trying to fill a UIView with a UIImageView using auto layout. 我正在尝试使用自动布局用UIImageView填充UIView。 I've tried several thing, but the most obvious, equalSize with parent view only works when I set the correct size in "Simulated metric". 我已经尝试了几件事,但最明显的是,只有当我在“模拟度量”中设置正确的大小时,父视图的equalSize才有效。 If I let Freeform (the point of autolayout isn't it ?) I get messed up. 如果我让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). 如你所见,我有4个边距约束,与你的代码相同(我不需要其余的因为我只使用主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. 无论如何,当我改变xcode的大小时,布局是完美的,当我将它发送到我的4S时,我只获得顶部和左边距。

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. 我会仔细检查UIView上的约束,你试图填充UIImageView以确保填充其父视图,因为它应该是在storyboard / xib设置为自由形式时。 Also if you're adding/creating the views programmatically double check that the views 'translatesAutoresizingMaskIntoConstraints is set to NO`. 此外,如果您以编程方式添加/创建视图,请仔细检查视图'translatesAutoresizingMaskIntoConstraints is set to NO`。 That always trips me up. 这总是让我失望。

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 这产生了 在iPhone6模拟器中查看纵向 and 在iPhone6模拟器中查看景观

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 . 注意在照片3,我选择我想约束,然后视图shift选择我想将它约束视图。 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 所以我选择了内部UIView (因为它的宽度/高度将被约束到父视图)然后shift选择其父视图(它嵌套在其中的视图)以启用Equal WidthEqual Height那些选项

Add constraints to UIImageView nested inside the UIView 向嵌套在UIView中的UIImageView添加约束 向嵌套在UIView中的UIImageView添加约束 Center UIView inside its parent view 在其父视图中居中UIView 在其父视图中居中UIView 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 . 添加宽度/高度限制等于父-注:我选择我想约束,然后视图shift选择我想将它约束视图。 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 所以我选择了内部UIView (因为它的宽度/高度将被约束到父视图)然后shift选择其父视图(它嵌套在其中的视图)以启用Equal WidthEqual Height那些选项 添加等于父级的宽度/高度约束 Change multiplier in constraint to be whatever you want, 0.5 in this case 将约束中的乘数更改为您想要的任何值,在这种情况下为0.5 将约束中的乘数更改为您想要的任何值,在这种情况下为0.5 Celebrate 庆祝 庆祝

i'm not sure but try this in your imageview. 我不确定,但在你的imageview中尝试这个。

[imgView clipToBounds:YES]; 

I hope it's work for you 我希望它对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM