2014-11-24 2 views
0

У меня есть UIView с UITableView внутри.Обновить размер UITableView на основе ограничений UIView

В UIViewController, я экземпляр UIView как это:

viewMain = [ViewMain alloc]initwithframe:cgrectmake(0,0,200,500)]; 

Внутри этой точки зрения, в методе initWithFrame: (CGRect) кадр, я экземпляр UITableView как это:

_tableView = [UITableView alloc]iniWithFrame:CGRectMake(0,0,frame.size.width,frame.size.height)]; 

Проблема в том, что когда я применяю ограничения к viewMain, как я могу обновить размер таблицыView на основе ограничений макета layoutMain?

ответ

1

вам нужно будет добавить ограничения на childview (т.е. autolayout сам, если изменения SuperView), например, так:

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]; 

Не уверен, что, если вы также должны сделать это:

_tableView.translatesAutoresizingMaskIntoConstraints = NO; 

перед добавлением ограничений.

0

Если вы настраиваете представление таблицы с рамкой, вы не сможете обновить макет с использованием ограничений. Если вы хотите обновить представление таблицы в соответствии с вашим родительским представлением, используйте ограничения для раскадровки, чтобы обновить ограничения.

0

В основном вы должны позвонить -setNeedUpdateConstraints и -updateConstraintsIfNeeded на соте в -tableView:cellForRowAtIndexPath:.

См. Очень подробное описание и код примера в this answer.

Отличный Tutorial by Ray Wenderlich.

Вы также можете попробовать это в вашем 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; 
} 
0

Я бы установить фрейм Tableview Равные к раме MainView в. i.e .:

_tableView = [UITableView alloc]iniWithFrame:CGRectMake(mainView.frame.origin.x,mainView.frame.origin.y,mainView.frame.size.width,mainView.frame.size.height)]; 
Смежные вопросы