Reputation: 93
I have a UIView with a UITableView inside.
In a UIViewController, I instantiate the UIView like this:
viewMain = [ViewMain alloc]initwithframe:cgrectmake(0,0,200,500)];
Inside this view, in the method initWithFrame:(CGRect)frame, I instantiate the UITableView like this:
_tableView = [UITableView alloc]iniWithFrame:CGRectMake(0,0,frame.size.width,frame.size.height)];
The problem is when I apply constraints to viewMain, how can I update de size of the tableView based on the viewMain layout constraints?
Upvotes: 0
Views: 1766
Reputation: 11
I would set the tableView's frame equal to the mainView's frame. i.e.:
_tableView = [UITableView alloc]iniWithFrame:CGRectMake(mainView.frame.origin.x,mainView.frame.origin.y,mainView.frame.size.width,mainView.frame.size.height)];
Upvotes: 0
Reputation: 7560
Basically you have to call -setNeedUpdateConstraints
and -updateConstraintsIfNeeded
on the cell in -tableView:cellForRowAtIndexPath:
.
See a very detailed explanation and sample code in this answer.
Here is a great Tutorial by Ray Wenderlich.
You can also try this in your tableViewController:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (IS_IOS8_OR_ABOVE) {
return UITableViewAutomaticDimension;
}
//self.prototypeCell.bounds = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(self.prototypeCell.bounds));
[self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];
[self.prototypeCell updateConstraintsIfNeeded];
[self.prototypeCell layoutIfNeeded];
return [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}
Upvotes: 0
Reputation: 770
you would need to add constraints to the childview (i.e. autolayout itself if the superview changes), like so:
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
[_tableView addConstraint:left];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
[_tableView addConstraint:top];
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[_tableView addConstraint:width];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:viewMain attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[_tableView addConstraint:height];
Not sure if you also need to do this:
_tableView.translatesAutoresizingMaskIntoConstraints = NO;
before adding the constraints.
Upvotes: 1
Reputation: 858
If you are setting table view with frame then you will not able to update layout using constraints. If you want to update your table view according to your parent view, use constraints from storyboard to update constraints.
Upvotes: 0