2015-03-21 8 views
0

Я хочу иметь UIView программно добавляемые к UITableViewController, который использует autolayout ...UITableViewController: программно autolayout ошибка на прошивке 7

У меня есть следующий код внутри из viewDidLoad из UITableViewController:

UIView *centerView = [[UIView alloc] init]; 
[centerView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
centerView.backgroundColor = [UIColor greenColor]; 
[self.view addSubview:centerView]; 

// Width constraint, half of parent view width 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                   attribute:NSLayoutAttributeWidth 
                   relatedBy:NSLayoutRelationEqual 
                   toItem:self.view 
                   attribute:NSLayoutAttributeWidth 
                  multiplier:0.5 
                   constant:0]]; 

// Height constraint, half of parent view height 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                   attribute:NSLayoutAttributeHeight 
                   relatedBy:NSLayoutRelationEqual 
                   toItem:self.view 
                   attribute:NSLayoutAttributeHeight 
                  multiplier:0.5 
                   constant:0]]; 

// Center horizontally 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                   attribute:NSLayoutAttributeCenterX 
                   relatedBy:NSLayoutRelationEqual 
                   toItem:self.view 
                   attribute:NSLayoutAttributeCenterX 
                  multiplier:1.0 
                   constant:0.0]]; 

// Center vertically 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                   attribute:NSLayoutAttributeCenterY 
                   relatedBy:NSLayoutRelationEqual 
                   toItem:self.view 
                   attribute:NSLayoutAttributeCenterY 
                  multiplier:1.0 
                   constant:0.0]]; 

Если я теперь запустить приложение на прошивке 8 все работает хорошо:

running on iOS 8


Если я запускаю его на прошивке 7, приложение падает со следующей ошибкой:

*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794

Если я поставил setTranslatesAutoresizingMaskIntoConstraints в YES, аварий приложений, а также с это уведомление: Unable to simultaneously satisfy constraints

ответ

4

Хорошо, это выглядит как UITableView не поддерживает Autolayout на прошивке 7. Таким образом, мы должны использовать маски автоматического изменения. Моя идея заключается в том, чтобы поставить containerView с маской в ​​качестве автоматического изменения подвида в Tableview и внутри, что ваш centerView:

Код:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setupBackgroundView]; 
} 

- (void)setupBackgroundView 
{ 
    UIView *backgroundContainerView = [[UIView alloc] initWithFrame:CGRectZero]; 
    backgroundContainerView.backgroundColor = [UIColor cyanColor]; 
    backgroundContainerView.frame = self.tableView.bounds; 
    backgroundContainerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    [self.view addSubview:backgroundContainerView]; 


    UIView *centerView = [[UIView alloc] initWithFrame:CGRectZero]; 
    centerView.translatesAutoresizingMaskIntoConstraints = NO; 
    centerView.backgroundColor = [UIColor greenColor]; 

    [backgroundContainerView addSubview:centerView]; 

    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeWidth 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeWidth 
                     multiplier:0.5 
                     constant:0]]; 

    // Height constraint, half of parent view height 
    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeHeight 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeHeight 
                     multiplier:0.5 
                     constant:0]]; 

    // Center horizontally 
    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeCenterX 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeCenterX 
                     multiplier:1.0 
                     constant:0.0]]; 

    // Center vertically 
    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeCenterY 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeCenterY 
                     multiplier:1.0 
                     constant:0.0]]; 

} 

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

- (void)setupBackgroundView 
{ 
    UIView *backgroundContainerView = [[UIView alloc] initWithFrame:CGRectZero]; 
    backgroundContainerView.backgroundColor = [UIColor cyanColor]; 

    UIView *centerView = [[UIView alloc] initWithFrame:CGRectZero]; 
    centerView.translatesAutoresizingMaskIntoConstraints = NO; 
    centerView.backgroundColor = [UIColor greenColor]; 

    [backgroundContainerView addSubview:centerView]; 

    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeWidth 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeWidth 
                     multiplier:0.5 
                     constant:0]]; 

    // Height constraint, half of parent view height 
    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeHeight 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeHeight 
                     multiplier:0.5 
                     constant:0]]; 

    // Center horizontally 
    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeCenterX 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeCenterX 
                     multiplier:1.0 
                     constant:0.0]]; 

    // Center vertically 
    [backgroundContainerView addConstraint:[NSLayoutConstraint constraintWithItem:centerView 
                     attribute:NSLayoutAttributeCenterY 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:backgroundContainerView 
                     attribute:NSLayoutAttributeCenterY 
                     multiplier:1.0 
                     constant:0.0]]; 

    self.tableView.backgroundView = backgroundContainerView; 

} 
+0

Perfect! Это хороший обходной путь! –

0

Попробуйте удалить эту строку из своего кода один раз и построить тогда.

[centerView setTranslatesAutoresizingMaskIntoConstraints:NO];

+0

получаю сообщение об ошибке: 'свойство contentview не найден на объект типа FavoritesViewController' -' FavoritesViewController' является 'UITableViewController' –

+1

@ Vamshi: Он хочет добавить представление в виде подсмотра в представлении ViewController, а не в UITableViewCell – tubtub

+0

@tubtub, это правильно! –