【问题标题】:UITableViewController: programmatically autolayout bug on iOS 7UITableViewController:iOS 7 上的编程自动布局错误
【发布时间】:2015-05-24 19:19:43
【问题描述】:

我想以编程方式将UIView 添加到使用自动布局的UITableViewController...

我在UITableViewControllerviewDidLoad 中有以下代码:

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

如果我现在在 iOS 8 上运行该应用程序一切正常:


如果我在 iOS 7 上运行它,应用程序会崩溃并出现以下错误:

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

如果我将setTranslatesAutoresizingMaskIntoConstraints 设置为YES,应用程序也会崩溃并显示以下通知:Unable to simultaneously satisfy constraints

【问题讨论】:

    标签: ios uitableview ios7 autolayout


    【解决方案1】:

    好的,iOS 7 上 UITableView 似乎不支持 Autolayout。所以我们必须使用自动调整大小的遮罩。我的想法是将带有自动调整大小掩码的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 在表格视图滚动时移动。如果你想修复它并且内容悬停在上面,你应该在UITableView上使用backgroundView

    - (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;
    
    }
    

    【讨论】:

      【解决方案2】:

      尝试从您的代码中删除此行一次,然后构建。

      [centerView setTranslatesAutoresizingMaskIntoConstraints:NO];

      【讨论】:

      • 出现错误:property contentview not found on object of type FavoritesViewController - FavoritesViewControllerUITableViewController
      • @Vamshi:他想将视图作为子视图添加到 ViewController 的视图,而不是 UITableViewCell
      • @Vamshi:删除 setTranslatesAutoresizingMaskIntoConstraints 后仍然崩溃,出现此错误:Unable to simultaneously satisfy constraints.
      猜你喜欢
      • 1970-01-01
      • 2015-01-23
      • 2013-03-07
      • 2016-12-14
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      相关资源
      最近更新 更多