0
votes

I have a parent VC that loads a child VC inside it. Code in parent VC is as follows:

self.draggerViewController = [[DraggerViewController alloc] initWithSuperView:self.view];

And the code of the child VC is as follows:

- (instancetype)initWithSuperView:(UIView*)superView {
    self = [super init];

    if (self) {
        self.superView = superView;

        [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
        [superView addSubview:self.view];

        [superView addConstraint:[NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];

        [superView addConstraint:[NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];

        self.constraintDraggerAttributeBottom = [NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [superView addConstraint:self.constraintDraggerAttributeBottom];
    }

    return self;
}

With this code, the subview is visible on its parent view and constraints are applied correctly so that it is placed at the bottom of the parent view with leading and trailing being 0. BUT, the real frame of the view is outside the parent view.

That is, I can see the subview at the bottom of the parent view but the frame of this subview is frame = (0 -315; 768 35).

If I set 'clipToBounds' to 'YES', the view is placed on the real frame but now constraints are not applied correctly.

How can I place this child VC inside the parent VC's view at the position I want using constraints?

Thank you!

2

2 Answers

1
votes

OK, got it...

I was forgetting the height constraint of the view... What a shame...

[superView addConstraint:[NSLayoutConstraint constraintWithItem:superView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];

Thank you all!

0
votes

Try this :

superView.insertSubview(self.view, at: self. superView.subviews.count)