Embedded a view and resize it

241 Views Asked by At

i'm having a problem with embedded views and auto layout.

I've created a view, which is a little complex. So now I want to refactoring this view and create some view components. I got one of the views and take together in one uiview class, and put all its logic there. Lets call this view as XView. All right until now.

So I tried to embed XView in the main view, to see the view works, with its new component. I put this commands:

xViewInstance = ...
[self.container addSubview:xViewInstance];

It doesn't work. the xViewInstance is bigger than the parent view. I want to resize xViewInstance.

So I googled for answers to see what's going wrong. And I found some answers that could helped me. I found PureLayout.

So I tried with it.

- (void)updateViewConstraints {

    if (!self.didSetupConstraints) {

        [self.xViewInstance autoPinEdgesToSuperviewEdges];

        self.didSetupConstraints = true;
    }
    [super updateViewConstraints];
}

It didn't work. xViewInstance continues bigger than its parent.

I found another answer here in stack, a code that create constraints in code, to adjusts subviews programmatically. Again it didn't work.

Now I have no ideia whats could be. I'm thinking that could some priority of the xViewInstance constraints.

Have someone ever passed for this situation? I would be very grateful if anyone can give some advice about this.

1

There are 1 best solutions below

5
On

I believe this post will solve your problem:

Use autolayout to set dynamic UIView to match container view

I tested it like this and it worked:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Init with a huge frame to see if it resizes.
    xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 800)];
    xView.translatesAutoresizingMaskIntoConstraints = NO;
    xView.backgroundColor = [UIColor redColor];

    [containerView addSubview:xView];
    [self addConstraints];
}

- (void)addConstraints
{
    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:xView
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:containerView
                                                              attribute:NSLayoutAttributeTop
                                                             multiplier:1.0
                                                               constant:0.0]];

    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:xView
                                                              attribute:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:containerView
                                                              attribute:NSLayoutAttributeLeading
                                                             multiplier:1.0
                                                               constant:0.0]];

    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:xView
                                                              attribute:NSLayoutAttributeBottom
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:containerView
                                                              attribute:NSLayoutAttributeBottom
                                                             multiplier:1.0
                                                               constant:0.0]];

    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:xView
                                                              attribute:NSLayoutAttributeTrailing
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:containerView
                                                              attribute:NSLayoutAttributeTrailing
                                                             multiplier:1.0
                                                               constant:0.0]];
}

Wg