3

Having two views headerView and footterView. I want the views to be placed one below another. i mean to say headerView on top and footerView below headerView.

UIView *mainview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    headerView =  [[[NSBundle mainBundle] loadNibNamed:@"BaseHeader" owner:self options:nil] objectAtIndex:0];
    footterView = [[[NSBundle mainBundle] loadNibNamed:@"BaseFotter" owner:self options:nil] objectAtIndex:0];

 self.view = mainview;
 [mainview  addSubview:headerView];
 [mainview  addSubview:footterView];

I have tried adding constraint as below

NSDictionary *viewsDictionary =
NSDictionaryOfVariableBindings(headerView,footterView);

NSLayoutConstraint *constraint = [[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerView]-[footterView]|" options:0 metrics:nil views:viewsDictionary] objectAtIndex:0];

NSLayoutConstraint *mconstraint = [[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerView(==footterView)]|" options:0 metrics:nil views:viewsDictionary] objectAtIndex:0];

[mainview addConstraint:constraint];
[mainview addConstraint:mconstraint];

But always the footerView alone is displayed in screen. If i add View in below order , always HeaderView is displayed.

[mainview  addSubview:footterView];
[mainview  addSubview:headerView];

AutoLayout is checked in both xib (BaseHeader.xib & BaseFotter.xib ). do i need to do any more in xib?

3
  • 1
    You need to set some horizontal constraints and/or a width (for one of the views) to make the constraints unambiguous. Commented Nov 16, 2013 at 6:18
  • Also, why are you using objectAtIndex:0 in your constraintsWithVisualFormat lines? That method creates an array of constraints, that you want to add to your view. Why are you adding just the first one? Commented Nov 16, 2013 at 6:32
  • ya. I should have added all constraints. i am adding array of constraints. Commented Nov 16, 2013 at 7:05

1 Answer 1

1

Change your constraints adding code to:

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerView]-[footterView]|" options:0 metrics:nil views:viewsDictionary];

NSArray *mconstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerView(==footterView)]|" options:0 metrics:nil views:viewsDictionary];

[mainview addConstraints: constraints]; [mainview addConstraints:mconstraints];

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.