Ошибка при попытке центрировать представление с помощью VFL с автозаполнением

#ios #autolayout

#iOS #автозаполнение

Вопрос:

Я пытаюсь центрировать представление в его супервизоре с помощью VFL, это мой код:

 - (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, используя just "|" вместо "view" ссылки на superview, я не получаю никаких ошибок, но выравнивание по вертикали не работает.

Есть идеи о том, что я делаю не так?

Ответ №1:

Попробуйте это

  - (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;
}
 

Комментарии:

1. Та же ошибка… Я полагаю, что проблема в том, что супервизор не имеет центра, когда я пытаюсь получить его из автозапуска.