2014-10-11 41 views
3

我想用自动布局填充UIImageView的UIView。我已经尝试了几件事,但最显而易见的与父视图相等的尺寸只有在“模拟度量”中设置了正确尺寸时才有效。如果我让Freeform(自动布局的重点不是它?)我会搞砸了。无法获得UIImageView用自动布局填充父UIView

我在iPhone 4S和6 plus上测试。

感谢您的任何主要曲目。

编辑@mittens

enter image description here

enter image description here

我看过你的编辑。我仍然可以得到它的工作。正如你所看到的,我有4个边界约束,和你的代码一样(我不需要其余部分,因为我只使用主UIView)。 无论如何,当我改变xcode的大小时,布局是完美的,当我将它发送给我的4S时,我只得到Top和左边距。

+0

所有约束你_look_像他们应该工作,它有'X','Y','width'&'height'约束。也许'UIImageView'模式设置为意想不到的事情?当你改变自由形式的大小时,侧面的自动布局会给你任何警告,如红色/黄色箭头/点类型的东西 – mittens 2014-10-11 23:45:01

回答

5

我会仔细检查的约束UIView你试图填补与UIImageView以确保它填补了其父视图所应当当storyboard/xib设置为自由形式时。另外,如果您要以编程方式添加/创建视图,请检查视图是否翻译了“自动更正大容量约束条件”is set to否。那总是让我trips trips不安。

我做了一个超快速查看并增加了一些限制,以两个视图控制器视图和图像视图内的视图展现给做这件事 - 希望这有助于至少有一点

- (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]; 
} 

这产生View in Portrait in iPhone6 simulatorView in Landscape in iPhone6 simulator

再次,希望这可以帮助。

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

注上照片3,我选择我要约束视图,然后shift选择我要约束它视图。因此,我所选择的内部UIView(因为它的宽度/高度将被约束到父视图),那么shift选择它的父视图(它嵌套的内侧的视图),以使的Equal Width & Equal Height

添加约束那些选项嵌套的UIImageView了UIView Add constraints to UIImageView nested inside the UIView 中心里面的UIView其父视图内 Center UIView inside its parent view 添加宽度/高度限制等于父 - 注:我选择我要约束视图,然后shift选择我要约束它的观点。因此,我所选择的内部UIView(因为它的宽度/高度将被约束到父视图),那么shift选择它的父视图(它嵌套的内侧的视图),以使在约束Equal Width & Equal Height Add width/height constraints equal to parent 变化乘数的那些选项是你想要的,0.5在这种情况下 Change multiplier in constraint to be whatever you want, 0.5 in this case 庆祝 Celebrate

+0

,谢谢你的答案。 我实际上是在Storyboard自动布局中做同样的事情,我尝试过: - Center X和Y,然后相同的高度相同的宽度 - 居中X和Y,然后约束左右上下边距 等等。 所以我尝试了你给我看的程序化的方式,它的工作。 你能告诉我如何在故事板上做同样的事情吗? 再次感谢 – OthmanT 2014-10-11 19:40:47

+1

@OthmanTazi编辑添加故事板屏幕截图。不确定您是否收到编辑通知。 – mittens 2014-10-11 19:54:07

+0

这是基本上我以前做过的事情:右拖动cliq来设置视图和图像视图之间的约束。你能看到我的编辑,我画了我的约束,所以你可以看到。 – OthmanT 2014-10-11 20:17:20

0

我不确定,但在imageview中试试这个。

[imgView clipToBounds:YES]; 

我希望这工作你

+0

已经尝试过,但首先感谢 – OthmanT 2014-10-11 18:40:11