0

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 Answers1

0

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

Community
  • 1
  • 1
Wizgod
  • 91
  • 10
  • Actually is this code that I mentioned in my question. It doesn't work for my case. > I found another answer here in stack, a code that create constraints in code, to adjusts subviews programmatically. Again it didn't work. – Guilherme Caraciolo Dec 04 '15 at 17:04
  • Could subviews in your view have constraints that might be causing it not to resize? How about the Content Hugging and Compression Resistance Priorities? – Wizgod Dec 04 '15 at 18:27
  • Maybe. I tried to take out all constraint and test it. It didn't work. – Guilherme Caraciolo Dec 04 '15 at 20:54
  • Did you create the view programmatically? If not, would you be able to attach a nib of it? – Wizgod Dec 05 '15 at 06:44
  • The view is create with nib format, then I alloc it, and attach it in a container view. – Guilherme Caraciolo Dec 06 '15 at 14:39