有两个视图headerView和footterView。我希望将视图放在另一个下面。 我的意思是说headerView在topView和footerView下面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];
我尝试过添加约束
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];
但是屏幕上始终只显示footerView。 如果我按以下顺序添加View,则始终显示HeaderView。
[mainview addSubview:footterView];
[mainview addSubview:headerView];
在xib(BaseHeader.xib和BaseFotter.xib)中都检查了AutoLayout。我需要在xib中做更多吗?
答案 0 :(得分:1)
更改约束,将代码添加到:
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];