2016-02-12 65 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

相關問題