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

Oth*_*anT 3 iphone xcode objective-c uiimageview autolayout

我正在尝试使用自动布局用UIImageView填充UIView.我已经尝试了几件事,但最明显的是,只有当我在"模拟度量"中设置正确的大小时,父视图的equalSize才有效.如果我让Freeform(自动布局点不是吗?)我搞砸了.

我正在测试iPhone 4S和6 plus.

感谢任何领先的曲目.

编辑@mittens

在此输入图像描述

在此输入图像描述

我见过你的编辑.我仍然可以让它工作.如你所见,我有4个边距约束,与你的代码相同(我不需要其余的因为我只使用主UIView).无论如何,当我改变xcode的大小时,布局是完美的,当我将它发送到我的4S时,我只获得顶部和左边距.

mit*_*ens 5

我会仔细检查UIView你试图填充的约束,UIImageView以确保它填充其父视图,因为它应该是当storyboard/xib设置为自由形式时.此外,如果您以编程方式添加/创建视图,请仔细检查视图'translatesAutoresizingMaskIntoConstraints is set toNO`.这总是让我失望.

我做了一个超快速的视图,并为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];
}
Run Code Online (Sandbox Code Playgroud)

这产生了 在iPhone6模拟器中查看纵向在iPhone6模拟器中查看景观

再次,希望这会有所帮助.

编辑:如何在故事板中添加它们

注意在照片3,我选择我想约束,然后视图shift中选择我想它约束的观点.所以我选择了内部UIView(因为它的宽度/高度将被约束到父视图)然后shift选择其父视图(它嵌套在其中的视图)以启用Equal Width&的那些选项Equal Height

向嵌套在UIView中的UIImageView添加约束 向嵌套在UIView中的UIImageView添加约束 在其父视图中居中UIView 在其父视图中居中UIView 添加宽度/高度限制等于父-注:我选择我想约束,然后视图shift中选择我想它约束的观点.所以我选择了内部UIView(因为它的宽度/高度将被约束到父视图)然后shift选择其父视图(它嵌套在其中的视图)以启用Equal Width&的那些选项Equal Height 添加等于父级的宽度/高度约束 将约束中的乘数更改为您想要的任何值,在这种情况下为0.5 将约束中的乘数更改为您想要的任何值,在这种情况下为0.5 庆祝 庆祝