2014-06-20 16 views
0

我試圖使用中心VFL它的父中的觀點,這是我的代碼:錯誤嘗試使用中心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)' 

如果我使用的只是"|"代替"view"來指代上海華改變VFL我沒有得到任何錯誤,但垂直對齊方式不起作用。

任何關於我在做什麼錯的想法?

回答

-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; 
} 
+0

同樣的錯誤......我想,問題是上海華還沒有一箇中心,當我嘗試從自動版式得到它。 – MatterGoal