2016-02-12 62 views
0

首先,这不是关于动态单元格的高度。所以不要混淆。Autolay + +根据总行数限制动态桌面高度

我有一个场景我创建了三个卡

  1. 详细信息卡:用于位置显示具体细节
  2. 图卡:展示基于选择
  3. 更多细节卡不同的图表:卡显示更多详情

下面是上述卡屏幕:

以上屏幕

enter image description here enter image description here enter image description here

查看层次结构:

  • 控制器
    • 滚动型
    • 卡-1
    • 卡-2
    • 卡-3 < ----我们有兴趣在此卡
      • 自定义视图
        • 的TableView

所以我展示这里最后一张卡片的限制,以上所有卡片都完美无缺!并且没有限制歧义或警告。

让我们通过代码:

首先添加Card-3ScrollView这里chartBox是卡-2和bottomBox是卡-3

[self.scrollView addConstraints:[NSLayoutConstraint 
           constraintsWithVisualFormat:@"V:[chartBox(200)]-(10)-[bottomBox]" 
           options:0 
           metrics:nil 
           views:@{@"bottomBox" : self.bottomBox, @"chartBox" : self.chartBox}]]; 

// Below constraint pin to increase content-size 
[self.scrollView addConstraints:[NSLayoutConstraint 
           constraintsWithVisualFormat:@"V:[bottomBox]-(10)-|" 
           options:0 
           metrics:nil 
           views:@{@"bottomBox" : self.bottomBox}]]; 

[self.scrollView addConstraints:[NSLayoutConstraint 
           constraintsWithVisualFormat:@"H:[bottomBox]-(10)-|" 
           options:0 
           metrics:nil 
           views:@{@"bottomBox" : self.bottomBox}]]; 

二加CustomViewCard-3这里bluePrintView是CustomView

_bluePrintView = [[BluePrintView alloc] init]; 
self.bluePrintView.translatesAutoresizingMaskIntoConstraints = NO; 
[self.bottomBox addSubview:self.bluePrintView]; 

[self.bottomBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bluePrintView]|" options:0 metrics:nil views:@{@"bluePrintView":self.bluePrintView}]]; 
[self.bottomBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[bluePrintView]|" options:0 metrics:nil views:@{@"bluePrintView":self.bluePrintView}]]; 

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width - 20; 
NSDictionary *metrices = @{@"width" : [NSNumber numberWithFloat:screenWidth]}; 
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[bottomBox(width)]" options:0 metrics:metrices views:@{ @"bottomBox": self.bottomBox}]]; 

三加TableViewCustomView

self.tableView.tableFooterView = [[UIView alloc] init]; 

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:@{@"tableView":self.tableView}]]; 
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:nil views:@{@"tableView":self.tableView}]]; 

// Height constraint 
CGFloat viewHeight = self.bounds.size.height; 
self.tableHeightConstraint = [NSLayoutConstraint constraintWithItem:self.tableView 
                  attribute:NSLayoutAttributeHeight 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:nil 
                  attribute:NSLayoutAttributeNotAnAttribute 
                 multiplier:1.0 
                  constant:viewHeight]; 
[self addConstraint:self.tableHeightConstraint]; 

[self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL]; 

而在观察者方法我会更新tableHeightConstraint约束。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    self.tableHeightConstraint.constant = self.tableView.contentSize.height; 
    [self.tableView setNeedsLayout]; 
    [self.tableView layoutIfNeeded]; 
} 

尝试所有可能性后,在这里结束。警告约束。

(
    "<NSLayoutConstraint:0x7fbeb1e7e8e0 V:|-(0)-[UITableView:0x7fbeb2843000] (Names: '|':BluePrintView:0x7fbeb1e7ac30)>", 
    "<NSLayoutConstraint:0x7fbeb1e7e700 V:[UITableView:0x7fbeb2843000]-(0)-| (Names: '|':BluePrintView:0x7fbeb1e7ac30)>", 
    "<NSLayoutConstraint:0x7fbeb1e7e9b0 V:[UITableView:0x7fbeb2843000(480)]>", 
    "<NSLayoutConstraint:0x7fbeb1e81a20 '_UITemporaryLayoutHeight' V:[BluePrintView:0x7fbeb1e7ac30(0)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fbeb1e7e700 V:[UITableView:0x7fbeb2843000]-(0)-| (Names: '|':BluePrintView:0x7fbeb1e7ac30)> 

我解决了这些警告通过从[tableView]|行(除去底部约束到上海华盈),但随后的tableView没有得到渴望的高度。

任何想法如何获得完整的tableView高度与任何约束警告。

P.S.所有视图都是由代码创建的,没有XIB没有故事板布局。 iOS 9.2

回答

1

我在您的约束设置中没有看到任何错误。

您可能会看到这个UITemporaryLayoutHeight约束,因为你在视图的生命周期,这将触发[self.tableView layoutIfNeeded]太早添加contentSize观察者([self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL] )。

尝试将观察者添加到viewDidLoad中,将其删除到View Controller的dealloc中;或者在您的viewWillAppear中,并将其除去viewWillDisappear为例。

+0

是的,你说得对。该观察员很快在生命周期中加入。我使用了视图生命周期方法完成执行后添加计时器和延迟0.5秒的解决方案。它的工作原理。 – Kampai

相关问题