My code crashes when I added a subview to a table view, using the AutoLayout technique, and here is my code:
@interface GNViewController () {
UITableView *_tvResults;
UIView *_view;
}
@end
@implementation GNViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
_tvResults = [UITableView new];
_tvResults.backgroundColor = [UIColor greenColor];
[self.view addSubview:_tvResults];
_view = [UIView new];
_view.backgroundColor = [UIColor redColor];
[_tvResults addSubview:_view];
_tvResults.translatesAutoresizingMaskIntoConstraints = NO;
_view.translatesAutoresizingMaskIntoConstraints = NO;
NSArray *cs = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_tvResults]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tvResults)];
[self.view addConstraints:cs];
cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tvResults]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tvResults)];
[self.view addConstraints:cs];
cs = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view)];
[_tvResults addConstraints:cs];
cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view)];
[_tvResults addConstraints:cs];
}
@end
The crash message reads:
2014-03-14 22:45:06.801 asdjasjdasdasd[3758:a0b] * Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2903.2/UIView.m:8536 2014-03-14 22:45:06.805 asdjasjdasdasd[3758:a0b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
Anyone please help me, thanks in advance!