Can't get UIImageView to populate parent UIView with autoplay

I am trying to populate a UIView with a UIImageView using auto layout. I've tried a few things, but the most obvious one, equalSize with parent only works when I set the correct size in "Simulated metric". If I enable Freeform (auto-disconnect point right?) I get confused.

I am testing iPhone 4S and 6 Plus.

Thanks for any lead tracks.

EDIT FOR @mittens

enter image description here

enter image description here

I saw your edit. I can still get it to work. As you can see, I have 4 field constraints, the same as in your code (I didn't need the rest, because I only use the main UIView). Anyway, when I resize on xcode the layout is perfect, when I submit it to 4S I only get top and left margins.

+3


source to share


2 answers


I would double check the constraints on UIView

which you are trying to fill with UIImageView

to make sure it populates the parent view as it should when the storyboard / xib is set to freeform. Also if you add / create views programmatically, double check that the views will translate is set to

AutoresizingMaskIntoConstraints NO`. It always fails me.

I did a super quick view and added some constraints for both the view inside the View Controllers and the image view to show one way to do this - hopefully this helps at least a little

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

      

This creates View in Portrait in iPhone6 ​​simulatorandView in Landscape in iPhone6 ​​simulator



Again, hopefully this helps.

EDIT: how to add them to the storyboard

Note in photo 3, I select the view I want to constrain and then shift

select the view I want to snap it to. So I chose inside UIView

(since the width / height would be limited by the parent view) then I shift

selected its parent view (the view nested inside) to enable those options Equal Width

andEqual Height

Add constraints to UIImageView nested in UIView Add constraints to UIImageView nested inside the UIView Center of UIView inside parent view Center UIView inside its parent view Add width / height constraints equal to parent. Note. I select the view that I want to constrain and then shift

select the view that I want to bind it to. So I chose inside UIView

(since the width / height would be limited by the parent view) then shift

selected its parent view (the view nested inside) to enable these options Equal Width

and Equal Height

Add width / height constraints equal to parent Change the multiplier in the constraint to whatever you want 0.5 in this case Change multiplier in constraint to be whatever you want, 0.5 in this case celebrate Celebrate

+5


source


I'm not sure, but try this in your image.

[imgView clipToBounds:YES]; 

      



I hope this works for you.

0


source







All Articles