首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于总行的动态表高的Autolayout +约束

基于总行的动态表高的Autolayout +约束
EN

Stack Overflow用户
提问于 2016-02-12 12:59:44
回答 1查看 1.2K关注 0票数 0

,首先,这与动态细胞的高度无关。所以别搞混了。

我有一个场景,我创造了三张卡片

  1. 详细信息卡:显示特定位置的详细信息
  2. 图表卡:根据选择显示不同的图表
  3. 更详细信息卡:卡片显示更多详细信息

下面是上面卡片的屏幕:

以上屏幕的视图层次结构:

  • 控制器
    • ScrollView
    • 卡片-1
    • 卡片-2
    • 卡片-3<--我们对这张卡片感兴趣
      • 自定义视图
        • TableView

所以我在这里展示了最后一张卡的约束条件,所有上面的卡片都是完美的!没有任何限制、模糊或警告。

让我们看一看代码:

Card-3 首先将添加到 ScrollView**:**中,chartBox是Card-2,bottomBox是Card-3。

代码语言:javascript
复制
[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}]];

CustomView 第二次将添加到 Card-3**:** Here bluePrintView is CustomView

代码语言:javascript
复制
_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}]];

第三,在TableView CustomView**:**中添加

代码语言:javascript
复制
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约束。

代码语言:javascript
复制
- (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];
}

在尝试了所有的可能性之后,就到了这里。关于约束的警告。

代码语言:javascript
复制
(
    "<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]行(通过将底部约束移除到superView)来解决这些警告,但是tableView没有得到所需的高度。

知道如何在没有任何约束警告的情况下获得完整的tableView高度。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-12 14:05:05

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

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

尝试在viewDidLoad中添加观察者,在视图控制器的dealloc中删除它;或者在viewWillAppear中删除它,例如在viewWillDisappear中删除它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35363292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档