以编程方式UITableView tableHeaderView自动布局

时间:2014-11-07 05:43:49

标签: ios objective-c uitableview nslayoutconstraint

我想将布局约束添加到tableHeaderView

UIView *headerView = [[UIView alloc] init];
headerView.translatesAutoresizingMaskIntoConstraints = NO; // THIS WILL CRASH
headerView.backgroundColor = [UIColor clearColor];
_tableView.tableHeaderView = headerView;

UIView *headerWrapperView = [[UIView alloc] init];
headerWrapperView.translatesAutoresizingMaskIntoConstraints = NO;
headerWrapperView.backgroundColor = [UIColor whiteColor];
[headerView addSubview:headerWrapperView];

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

如果我删除headerView.translatesAutoresizingMaskIntoConstraints = NO;这一行, 然后我得到

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSLayoutConstraint:0x797f54f0 H:|-(20)-[UITextField:0x797ed440]   (Names: '|':UIView:0x797ea9e0 )>",
"<NSLayoutConstraint:0x797f5540 H:[UITextField:0x797ed440]-(20)-|   (Names: '|':UIView:0x797ea9e0 )>",
"<NSLayoutConstraint:0x797f5270 H:|-(10)-[UIView:0x797ea9e0]   (Names: '|':UIView:0x797ea800 )>",
"<NSLayoutConstraint:0x797f52e0 H:[UIView:0x797ea9e0]-(10)-|   (Names: '|':UIView:0x797ea800 )>",
"<NSAutoresizingMaskLayoutConstraint:0x79729910 h=--& v=--& H:[UIView:0x797ea800(0)]>"
)

有人解决此问题吗?可以举例吗?

P / S:headerWrapperView

中还有其他观点

更新

我尝试对headerView及其所有子视图使用非自动布局,tableView保持自动布局

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 400)];
headerView.backgroundColor = [UIColor clearColor];
_tableView.tableHeaderView = headerView;

NSLog(@"headerView %@", headerView);

然后我得到了这个结果

headerView <UIView: 0x79c38430; frame = (0 0; 0 400); layer = <CALayer: 0x79c38490>>

如果没有此行(下面一行)

_tableView.tableHeaderView = headerView;

然后我得到

headerView <UIView: 0x79c38430; frame = (0 0; 320 400); layer = <CALayer: 0x79c38490>>

2 个答案:

答案 0 :(得分:0)

来自此错误报告

NSAutoresizingMaskLayoutConstraint:0x79729910 h = - &amp; V = - &安培; H:[UIView的:0x797ea800(0)]

我认为如果删除该行,该视图的宽度为0

尝试使用框架CGRectMake(0,0,self.view.frame.size.width,300)初始化该视图

答案 1 :(得分:0)

[self.view addSubview:self.tableView];
 self.tableView.translatesAutoresizingMaskIntoConstraints = false;

[self.view addConstraints:@ [ [NSLayoutConstraint ConstraintWithItem:self.tableView属性:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual等于Item:self.view属性:NSLayoutAttributeLeading乘数:1常数:0],

         [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTrailing    relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0],

         [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop    relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0],

         [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom    relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0],
     ]];

///这很重要

     dispatch_async(dispatch_get_main_queue(), ^{
         self.tableView.tableHeaderView = self.topV;
     });

}