Ios 尝试使用带自动布局的VFL将视图居中时出错

Ios 尝试使用带自动布局的VFL将视图居中时出错,ios,autolayout,Ios,Autolayout,我正在尝试使用VFL将视图居中于其superview中,以下是我的代码: - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // THE SUPERVIEW UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, [self tableView:t

我正在尝试使用VFL将视图居中于其superview中,以下是我的代码:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // THE SUPERVIEW 
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, [self tableView:tableView heightForHeaderInSection:section])];
    sectionView.backgroundColor = [UIColor grayColor];

    // THE LABEL TO BE CENTERED
    UILabel *sectionTitle = [UILabel new];
    sectionTitle.translatesAutoresizingMaskIntoConstraints = NO;
    sectionTitle.Text = @"Generals";
    [sectionView addSubview:sectionTitle];

    // AUTOLAYOUT INFO 
    NSDictionary *viewsDictionary = @{@"label":sectionTitle, @"view":sectionView};
    NSArray *leftPosition = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view]-[label]"
                                                                    options:NSLayoutFormatAlignAllCenterY
                                                                    metrics:nil
                                                                      views:viewsDictionary];

    [sectionView addConstraints:leftPosition];

    return sectionView;
}
使用这个代码我得到了这个错误

2014-06-20 09:44:20.938 gogo[442:60b] *** Assertion failure in -[NSLayoutConstraint constant], /SourceCache/Foundation/Foundation-1047.25/Layout.subproj/NSLayoutConstraint.m:601
2014-06-20 09:44:24.651 gogo[442:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '(null)'
如果我只使用|而不是视图来更改VFL以引用superview,我不会得到任何错误,但垂直对齐不起作用

知道我做错了什么吗

试试这个

 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
...

[sectionView addConstraint:[NSLayoutConstraint constraintWithItem: sectionTitle
                                                        attribute: NSLayoutAttributeCenterY
                                                        relatedBy: NSLayoutRelationEqual
                                                           toItem: sectionView
                                                        attribute: NSLayoutAttributeCenterY
                                                       multiplier: 1.0f
                                                         constant: 0.0f]];
}
编辑:

尝试此操作后,UILabel将自动以Y为中心:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// THE LABEL TO BE CENTERED
     UILabel *sectionTitle = [UILabel new];
     sectionTitle.Text = @"Generals";
     return sectionTitle;
}

同样的错误。。。我想问题是当我试图从AutoLayout获取superview时,它没有一个中心。